我使用此代码使用户标记和主题标签可以在我的小网站上工作。Preg替换HTML撇号
$string = $text;
$pattern = '/(^|\W)(@([a-zA-Z0-9_\-]+))/';
$replacement = '$1<a class="blue" href="/app/profile/$3">$2</a>';
$text = preg_replace($pattern, $replacement, $string);
$string = $text;
$pattern = '/(^|\W)(#([a-zA-Z0-9_\-]+))/';
$replacement = '$1<a class="blue" href="/app/find/search/$3">$2</a>';
$text = preg_replace($pattern, $replacement, $string);
echo $text;
我的数据库保存这样的文本的所有作品:
he's
(他)
然而,当它输出通过这个代码的话不归,输出如下:
he&<a class="blue" href="/app/find/search/039">#039</a>;s
当shold只是 “他” 怎能时I g等preg取代它看作是一个撇号?
你没有让'&'和','在'$ pattern',所以他们被剥离出来。 –
在我的回答中有没有你不明白的地方?我没有看到任何评论。 –