Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace everything between two tags [duplicate]

Tags:

regex

php

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']);
like image 556
www.data-blogger.com Avatar asked Jan 17 '23 06:01

www.data-blogger.com


1 Answers

You are matching greedily.

You need a non-greedy modifier:

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

Notice that .* has become .*?.

like image 197
Mark Byers Avatar answered Jan 18 '23 23:01

Mark Byers