preg_replace包含使用的“+”符号IF

时间:2014-03-01 19:14:42

标签: php regex preg-replace posts

我目前的代码是:

$epattern[17] = "/@(\\w+)/"; 
$ereplace[17] = "<a href=viewprofile.php?username=$1><font color=royalblue><b>@\\1</b></font></a>";
$postinforawb = preg_replace($epattern,$ereplace,$postinfo);

使用上面的代码,文本将突出显示蓝色,其中@符号用于输入空格的位置。但是我现在也希望它在帖子中包含“+”符号。以下将突出显示蓝色:“@ First + Second”

我需要添加什么来替换?

2 个答案:

答案 0 :(得分:2)

这将适用于您的情况:

$epattern[17] = "/@([\w\+]+)/"; 

但我更喜欢这个,因为你只允许使用字母和+

$epattern[17] = "/@([a-zA-Z\+]+)/"; 

答案 1 :(得分:1)

$epattern[17] = "/@([\w\+]+)/";