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


Find result str with regular expressions in php

有一个输入字符串:

$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";

哪个正则表达式会简单地删除我指定的所有符号?我如何以正确的方式过滤它?谢谢!

您不需要正则表达式:

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

您可能想要trim结果字符串

下面是一个使用正则表达式的例子:

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

输出:

some usefull text and garbage