0

I'm having issues with a external link that has several ':' in it's link and is not working correct. Via urlencode I can change this character to %3A which works, but inside a it pastes the link behind the URL the user is currently viewing.

The external link (in this case $url) looks like this:

https://prf.hn/click/camref:somestring/creativeref:somestring/destination:https://www.example.org/product/productname.html

I currently try to print it the $url this way:

<a href="<?php echo urlencode(html_entity_decode($url) ) ; ?>">

This currently results in, which doesn't work:

https://example.net/page/https%3A%2F%2Fprf.hn%2Fclick%2F

Instead of, what should work:

https://prf.hn%2Fclick%2F

What am I doing wrong?

rwzdoorn
  • 615
  • 8
  • 29
  • I cannot think of one original `$url` that could create both results you mention. Have you checked your own HTML source code in the browser? Why not put the original `$url` in your question? It would also be nice to know why you want to encode an url in the first place. – KIKO Software Jan 26 '21 at 09:14

2 Answers2

1

Difficult to see what you are trying to do here. When you say the link has a ":" in it, do you mean the colon in https://...?

Calling urlencode will convert the colon to %3F and / to %2F so the "url" string is no longer seen as a valid url. Clicking on it will cause your browser to look for that string as a file in the current path, and so prefix it with the page's path.

Take a look at the developer tools/view source of the page you are generating and I think you'll see something like

<a href="https%3A%2F%2Fprf.hn%252Fclick%252F">Test</a>

... which isn't a url.

Are you starting with $url="https://prf.hn/click/" or "https://prf.hn%2Fclick%2F" ?

If the first, why the decode and encode?

If the second, using just html_entity_decode($url) gives https://prf.hn/click/ which IS a valud url.

ajCary
  • 64
  • 5
  • Great effort trying to explain what this question is about. – KIKO Software Jan 26 '21 at 09:44
  • Hi AjCary, thanks for the notice. I added a few examples. The problem is: unicode works, but it pastes the external link behind my own url in the – rwzdoorn Jan 27 '21 at 08:57
  • That does help, thanks. So if I'm not missing something, the part of the url after the /click/ is several identifier:value where the last one's value is a url. Hmmm. Well, ":" is a valid character in a url - see https://stackoverflow.com/a/2053640/2493082 , so maybe all you need to do is deal with encoding the destination:url bit. – ajCary Jan 27 '21 at 16:19
1
$url="https://prf.hn/click/camref:somestring/creativeref:somestring/destination:https://www.example.org/product/productname.html";
preg_match_all( '@https?://@', $url, $matches, PREG_OFFSET_CAPTURE); // is there a URL in $url?
$new_url = $url;
if (count($matches[0])>1) // more than one "https://" in url
{
   $new_url = substr($url, 0, $matches[0][1][1]) . urlencode(substr($url, $matches[0][1][1]));
}
echo $new_url;

Outputs: https://prf.hn/click/camref:somestring/creativeref:somestring/destination:https%3A%2F%2Fwww.example.org%2Fproduct%2Fproductname.html

Does that get you any closer?

ajCary
  • 64
  • 5