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']);
You are matching greedily.
You need a non-greedy modifier:
'/<!--[ ]*START[ ]*NO[ ]*PRINT[ ]*-->(.*?)<!--[ ]*END[ ]*NO[ ]*PRINT[ ]*-->/si'
Notice that .*
has become .*?
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With