Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

php urlencode pastes home + other domain in one url

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?

question from:https://stackoverflow.com/questions/65898257/php-urlencode-pastes-home-other-domain-in-one-url

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
$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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...