I am trying to parse urls containing &using preg_replace.
$content = preg_replace('#https?://[a-z0-9._/\?=&-]+#i', '<a href="$0" target="_blank">$0</a>', $content);
But I use it for user comments, so I also use the htmlspecialchars () function to prevent XSS.
function formatContributionContent($content)
{
$content = nl2br(htmlspecialchars($content));
$content = preg_replace('#[a-z0-9._-]+@[a-z0-9._&-]{2,}\.[a-z]{2,4}#', '<a href="mailto:$0">$0</a>', $content);
$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');
And htmlspecialchars converts &to "&", so my regular expression produces the wrong result. Indeed, with the following URL.
http:
I get ;
<a href="https://openclassrooms.com/index.php?page=3&" target="_blank">https:
source
share