php preg_replace everything in between specific html comment tags

前端 未结 2 541
执念已碎
执念已碎 2021-01-03 09:30

I\'ve checked other answers but can\'t seem to do the following. Please help someone :)

I want to remove everything in between and including specific html comments

相关标签:
2条回答
  • 2021-01-03 10:10

    That regex looks fine. Use an m modifier to make the dot match newlines:

    "/<!-- START IF USER_ID -->.*?<!-- END IF USER_ID -->/m"
    

    Alternatively, you could use [\s\S] as a substitute:

    "/<!-- START IF USER_ID -->[\s\S]*?<!-- END IF USER_ID -->/"
    
    0 讨论(0)
  • Thanks @mlwacosmos - Using the link you provided.

    Achieved with:

    $startPoint = '<!-- START IF USER_ID -->';
    $endPoint = '<!-- END IF USER_ID -->';
    $result = preg_replace('#('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')#siU', '', $html);
    
    0 讨论(0)
提交回复
热议问题