4

Possible Duplicate:
Insert string between two points with PHP

How can I replace everything between <!-- START NOT PRINT --> and <!-- END NO PRINT -->?

The following code works well, but whenever there are two or more instances, it goes wrong.

It then replaces everything between the first tag and the last tag. But it should remove everything between two tags which belongs together. This is my code:

$pageData['raw_content'] = preg_replace('/<!--[ ]*START[ ]*NO[ ]*PRINT[ ]*-->(.*)<!--[ ]*END[ ]*NO[ ]*PRINT[ ]*-->/si', '', $pageData['raw_content']);
4

1 回答 1

9

你在贪婪地匹配。

你需要一个非贪婪的修饰符

'/<!--[ ]*START[ ]*NO[ ]*PRINT[ ]*-->(.*?)<!--[ ]*END[ ]*NO[ ]*PRINT[ ]*-->/si'

注意.*已经变成了.*?

于 2012-06-28T14:43:38.913 回答