php preg_replace特定html评论标签之间的所有内容

Oni*_*sha 3 php regex preg-replace

我检查了其他答案,但似乎无法做到以下几点.请帮助别人:)

我想删除其中的所有内容,包括特定的HTML注释

HTML:

Some HTML that must stay
<!-- START IF USER_ID -->
some html that must go
<!-- END IF USER_ID -->
Some more HTML that's gotta stay
<!-- START IF USER_ID -->
this also needs to go
<!-- END IF USER_ID -->
Run Code Online (Sandbox Code Playgroud)

因此,两者之间的一切<!-- START IF USER_ID -->,并<!-- END IF USER_ID -->和自身需要去评论

我的preg_replace模式(这显然是错误的):

"/<!-- START IF USER_ID -->.*?<!-- END IF USER_ID -->/"
Run Code Online (Sandbox Code Playgroud)

结果应该是

Some HTML that must stay
Some more HTML that's gotta stay
Run Code Online (Sandbox Code Playgroud)

感谢提前检查和答案:)

Oni*_*sha 7

谢谢@mlwacosmos - 使用您提供的链接.

达到:

$startPoint = '<!-- START IF USER_ID -->';
$endPoint = '<!-- END IF USER_ID -->';
$result = preg_replace('#('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')#siU', '', $html);
Run Code Online (Sandbox Code Playgroud)