php url编码将主页 + 其他域合二为一 url

php urlencode pastes home + other domain in one url

我在使用外部 link 时遇到问题,它在 link 中有多个“:”并且无法正常工作。通过 urlencode,我可以将此字符更改为 %3A,但它会在用户当前正在查看的 URL 后面粘贴 link。

外部 link(在本例中为 $url)如下所示:

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

我目前尝试以这种方式打印 $url:

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

当前结果是,这是行不通的:

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

而不是,什么应该起作用:

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

我做错了什么?

很难看出你在这里试图做什么。当您说 link 中有一个“:”时,您是指 https://...?

中的冒号吗

调用 urlencode 会将冒号转换为 %3F 和 / 转换为 %2F,因此“url”字符串不再被视为有效的 url。单击它将使您的浏览器在当前路径中查找该字符串作为文件,因此在它前面加上页面路径。

查看您正在生成的页面的开发者 tools/view 源代码,我想您会看到类似

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

...这不是 url.

您是从 $url="https://prf.hn/click/" 还是 "https://prf.hn%2Fclick%2F" 开始的?

如果是第一个,为什么要解码和编码?

如果第二个,仅使用 html_entity_decode($url) 给出 https://prf.hn/click/ 这是一个值 url.

$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;

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

这会让你更亲近吗?