目前我使用的是
$str = preg_replace("/\([^\)]+\)/", "", $str); // Remove () and Content
$str = preg_replace("/\[[^\)]+\]/", "", $str); // Remove [] and Content
$str = preg_replace("/\{[^\)]+\}/", "", $str); // Remove {} and Content
$str = preg_replace("/[^a-zA-Z0-9]/", "", $str); // Remove all non-alphanumeric characters我想知道是否有一种方法可以将它们组合到一个正则表达式语句中
发布于 2012-04-05 00:08:06
您可以使用|将它们组合在一起:
(\([^\)]+\)|\[[^\]]+\]|\{[^\}]+\})|[^A-Za-z0-9])但在您的情况下,我通常只会遍历字符串并手动解析它。
使用“状态变量”($paren_open、$brace_open、$curly_open)了解是否忽略字符。
即使它看起来不是很快(因为您必须遍历每个字符),它也比正则表达式快得多,因为正则表达式会做一些更复杂的事情。
资源:
https://stackoverflow.com/questions/10014976
复制相似问题