0

I'm trying to search through database content, using PHP, to find everything including and between specific HTML comments. Here's an example of the content I'm searching for:

<!-- wp:stmu/program-locations -->
<div class="programDetails" class="wp-block-stmu-program-locations">
<h2>Location</h2><ul><li>On Campus</li></ul>
</div>
<!-- /wp:stmu/program-locations -->

It's always structured the same way, with a comment, div, h2, ul, (variable number of lis), and closing comment. I want the regex to find these outer comments as well as the inner content, so the whole chunk can be replaced.

I've tried

<?php
$matched = preg_match(
    "/<!-- wp:stmu\/program-locations -->.*?<!-- \/wp:stmu\/program-locations -->/sg",
    $record->post_content,
    $matches
);
?>

which matches the desired content on regex.com, but on my server it does not return any matches. If I simplify to

<?php
$matched = preg_match(
    "<!-- wp:stmu\/program-locations -->",
    $record->post_content,
    $matches
);
?>

then I get matches, but it only matches !-- wp:stmu/program-locations -- - the actual < and > characters that wrap the comment are missing from the match. My PHP version is 7.3.13.

WebElaine
  • 299
  • 6
  • 16

0 Answers0