这消除评论区块:
preg_replace('%/\*\s+comment\s+.*?\*/%s', '', $string)
这GET的摆脱陈旧的空白,以及:
preg_replace('%/\s*\*\s+comment\s+.*?\*/\s*%s', '', $string)
这是一个测试脚本:
#!/usr/bin/php
<?php
$string = <<<EOS
/* comment [comment goes here] */
/* comment please do not delete the lines below */
[I am a special line so I should not be changed ]
/* comment please do not delete the line above */
EOS;
print $string;
print "\n---\n";
print preg_replace('%/\*\s+comment\s+.*?\*/%s', '', $string);
print "\n---\n";
print preg_replace('%/\s*\*\s+comment\s+.*?\*/\s*%s', '', $string);
?>
输出与PHP 5.3.4:
/* comment [comment goes here] */
/* comment please do not delete the lines below */
[I am a special line so I should not be changed ]
/* comment please do not delete the line above */
---
[I am a special line so I should not be changed ]
---
[I am a special line so I should not be changed ]
多远你试过吗? – codaddict 2011-03-22 11:57:47
我已经尝试了几件事,我的正则表达知识是=看一只猴子试图弄清楚他的反射是什么 – Val 2011-03-22 11:59:22