从字符串中删除特殊字符

时间:2014-06-24 08:43:11

标签: php

我想从以下字符串

中删除特殊字符@
 $string="Test <a href='/group/profile/1'>@Test User</a> another test <a href='/group/profile/2'>@User Test</a>";

预期产出:

 <a href='/group/profile/1'>Test User</a> another test <a href='/group/profile/2'>User Test</a>"

这里我需要检查字符串中的每个锚标记,并且只需要在href中找到带有单词profile的锚标记,并且需要从链接文本中删除@。如果有{{1}在字符串中的锚标记之外,不应该删除它,只需要删除锚标记中的@

3 个答案:

答案 0 :(得分:3)

使用正则表达式:

$string = preg_replace('/(<a href.*profile.*>)@/U', '$1', $string);

注意不合理的(U)修饰符。

答案 1 :(得分:0)

尝试:

$string = <<<EOT

@Do not remove
Test <a href='/group/profile/1'>@Test User</a> another test <a href='/group/profile/2'>@User Test</a>
@metoo

EOT;


$string = preg_replace('/(\<a\s+.+?\>.*?)(\@)(.*?\<\/a\>)/','$1$3',$string);

var_dump($string);

这可能是大字符串的错误

http://us3.php.net//manual/en/function.preg-replace.php http://us3.php.net/manual/en/reference.pcre.pattern.syntax.php

答案 2 :(得分:0)

试试这个:

//从字符串

中删除@
 $string=str_replace('@','',$string);

//使用php的

的base64_encode函数编码字符串
 $string=base64_encode($string);

//使用php的base64_decode函数解码字符串

echo $string=base64_decode($string);            //Expected output