I would like to delete all comments with their contents with preg_replace().
Example:
$mDom =
<<<HTML
<html>
word1 <!-- word2 --> word3 <!-- word4 --> word5
</html>
HTML;
$mDom = preg_replace('/<!--.*-->/ius', '', $mDom);
var_dump( $mDom );
The example above will print: “word1 word5”
I want: “word1 word3 word5”
You need to use lazy (non-greedy) quantification:
Note the
?behind*. It causes the regex engine to stop as soon as a match is found, not trying to make the match as long as possible.Read more about it.
EDIT : You can also use a special modifier
U:(PHP Manual: Possible modifiers in regex patterns)