【发布时间】:2012-08-29 15:41:42
【问题描述】:
我在这里要做的是获取一个可能包含通常是代码注释的字符串,并将其替换为其他内容,尤其是将其包装在其他内容中。我很确定preg_replace 函数可以在这里工作,但我不知道从哪里开始使用正则表达式。例如:
Hello world //this is a comment
Testing //not testing
Test again
会变成
Hello world %//this is a comment%
Testing %//not testing%
Test again
preg_replace('???', '%$1%', $matches); 是我自己能想到的,非常感谢任何帮助!
【问题讨论】:
-
$0 或 \\0 匹配整个事物,$1 是第一个反向引用。
标签: php regex replace comments