在php中使用正则表达式查找结果str

时间:2011-11-09 20:16:18

标签: php regex preg-replace preg-match

我有一个输入字符串:

$str1 = "some usefull text and garbage `~#@!&^*(()}{./";
$str2 = "`~#@!&^*(()}{./";

$result = Exclude with regular expressions all symbols from str1, which are in str2.
$result = "some usefull text and garbage";

什么正则表达式只会删除我指定的所有符号?如何以正确的方式过滤它?感谢名单!

2 个答案:

答案 0 :(得分:5)

您不需要正则表达式:

$clean_str = str_replace(str_split($str2), '', $str1);

您可能希望trim生成的字符串。

答案 1 :(得分:3)

以下是使用正则表达式的示例:

$str1 = "some usefull text and garbage `~#@!&^*(()}{./";
$str2 = "`~#@!&^*(()}{./";

$pattern = preg_quote($str2,'/'); 
echo preg_replace('/'.$pattern.'/', "", $str1);

输出:

some usefull text and garbage