$re = '/(?|
\G(?!\A) # contiguous to the precedent match (inside a comment)
(?|
-{2,}+([^->][^-]*) # duplicate hyphens, not part of the closing sequence
|
(-[^-]+) # preserve isolated hyphens
|
-+ (?=-->) # hyphens before closing sequence, break the contiguity
|
-->[^<]* (*SKIP)(*FAIL) # closing sequence, go to next <, break contiguity
)
|
[^<]*<+ # reach the next < (outside comment)
(?> [^<]+ <+ )*? # all characters until <!-- (include)
(?: !-- \K | [^<]*\z\K (*ACCEPT) ) # new comment or end of the string
(?|
-*+ ([^->][^-]*) # possible hyphens not followed by >
|
-+ (?=-->) # hyphens before closing sequence, break the contiguity
|
-?+ ([^-]+) # one hyphen followed by >
|
-->[^<]* (*SKIP)(*FAIL) () # closing sequence, go to next <, break contiguity
) # () avoids a misterious bug in regex101, you can remove it
)
/x';
$str = '<!----------------- toto -->
<!-- toto ---------------------------->
<!---->
<!----->
<!--->>>>--->
asdsd sadasdsad sadasd asdasd asdasd --> asd asdasd
<!--- ->>>>>>
---------------------------------------
***************************************
AUGMENTATION BLUEPRINTS
***************************************
---------------------------------------
--->----
<!-- toto-toto -->
<!--OTHER IDEAS --
<anytaghere>
<!----PROBABLY NOT:---
<whatever>
--><!--';
$subst = "\1";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php