Replace Everything Inside Two Strings

Using the built in PHP regex function preg_replace and property encoding the two strings you’re searching for with preg_quote you can remove all of the text between to inside strings. This method actually removes the the matching HTML comments and everything inside of them from the $sourceString variable.

$updatedString = preg_replace('/'.preg_quote('<!--START_REMOVE-->').'[\s\S]+?'.preg_quote('<!--END_REMOVE-->').'/', '', $sourceString);

Leave a Reply

Your email address will not be published.