我试图用&解析包含preg_replace的url。

$content = preg_replace('#https?://[a-z0-9._/\?=&-]+#i', '<a href="$0" target="_blank">$0</a>', $content);

但是我用它来做用户评论,所以我也用htmlspecialchars()函数来防止xss。
function formatContributionContent($content)
{
    $content = nl2br(htmlspecialchars($content));

    // Regexp for mails
    $content = preg_replace('#[a-z0-9._-]+@[a-z0-9._&-]{2,}\.[a-z]{2,4}#', '<a href="mailto:$0">$0</a>', $content);

    // Regexp for urls
    $content = preg_replace('#https?://[a-z0-9._/\?=&-]+#i', '<a href="$0" target="_blank">$0</a>', $content);

    var_dump($content);
}

formatContributionContent('https://openclassrooms.com/index.php?page=3&skin=blue');

htmlspecialchars将&转换为"&amp;",因此我的regexp产生错误的结果。确实,有以下网址。
http://www.siteduzero.com/index.php?page=3&skin=blue

我得到;
<a href="https://openclassrooms.com/index.php?page=3&amp" target="_blank">https://openclassrooms.com/index.php?page=3&amp</a>;skin=blue

最佳答案

您可以在regexp匹配的字符列表中添加“;”,如下所示:

$content = preg_replace('#https?://[a-z0-9._/\?=&;-]+#i', '<a href="$0" target="_blank">$0</a>', $content);

这样,htmlspecialchars可以在"&"中转换"&amp;"字符,但是regexp可以找到整个url。

10-05 20:37
查看更多