PHP正则表达式替换除第一个之外的所有匹配项

时间:2013-11-30 14:42:24

标签: php regex

我正在使用preg_replace将替换为以下字符串中的 \ n#

#0 F:\dev\htdocs\cms\lib\Cms.php(76): 
ReflectionClass->__construct('Mod_Simple_Top_...') 
#1 F:\dev\htdocs\cms\cms.php(45): Cms::renderModules(Array, 
#2 F:\dev\htdocs\cms\bootstrap.php(63): require_once('F:\dev\htdocs\c...')
#3 {main}
  

preg_replace('/#/','\ n',$ error_message);

问题是它也取代了第一场比赛,即; #0 regexp中是否有任何语法,因此不应考虑第一个匹配。

2 个答案:

答案 0 :(得分:2)

“查找后跟”0“的所有哈希符号。

echo preg_replace('/(#)(?!0)/' , '\n', $str);

→ live example

特别是,群组(?!0)称为negative lookahead

答案 1 :(得分:1)

更换后只需先删除\ n。使用substr($string, 1)trim($string)

相关问题