Skip to content
Advertisement

PHP get the content after each h2 tag

I’m trying to use php to extract content after each h2 tag (and before the next h2 tag)..

Example:

$content = '<h2>title 1</h2>
<ul>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>
<h2>title 2</h2>
<p>testing only</p>
<p>testing only</p>
<p>testing only</p>
<h2>title 3</h2>
<p>testing only</p>
<p>testing only</p>';

To become

[0] => <ul>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>

[1] => <p>testing only</p>
<p>testing only</p>
<p>testing only</p>

[2] => <p>testing only</p>
<p>testing only</p>

I have tried so many different things, too many to list here. I only want to extract the content between the h2 tags, not the h2 tags themselves.

If anyone could please point me in the right direction, or help me out, that would be greatly appreciated!

Thank you.

Advertisement

Answer

Try this one 🙂

<?php

    $content = "your content";

    preg_match_all('/(?:</h2>)(.*?)(?:<h2>|z)/s', $content, $match);

    var_dump($match);
?>

Demo -> https://www.phpliveregex.com/p/x7j (select, preg_match_all)

Edit

Note, if you ask yourself why there is an multidimensional array as match result:

  • $matches[0] is an array of full pattern matches

  • $matches[1] is an array of strings matched by the first parenthesized subpattern.

  • $matches[2] is an array of strings matched by the second parenthesized subpattern.

  • (…), and so on

If you want to check if the preg_match_all was successful note to check $match[0] before you proceed. If you want to check you match groups note to check eg. $1 -> $match[1], $2 -> $match[2], $3 -> $match[3], (…) and so on;

If you match multiple times your match groups will contain more than one result.

Example: single match

https://phpsandbox.io/n/icy-term-9wp6

<?php
    $test_string = "Your task XX-123";

    preg_match_all('/task (([A-Z]{1,2})-([0-9]{1,}))/s', $test_string, $match);

    // destruct your array is equal to selection by index $match[$index]
    [$full_match, $match_group_1, $match_group_2, $match_group_3] = $match;

    var_dump($full_match);    // -> ["task XX-123"]
    var_dump($match_group_1); // -> ["XX-123"]
    var_dump($match_group_2); // -> ["XX"]
    var_dump($match_group_3); // -> ["123"]
?>

Example: multiple match

https://phpsandbox.io/n/shy-credit-0ng6

<?php
    $test_string = "Your task XX-123, Your task YZ-456, Your task CD-789";

    preg_match_all('/task (([A-Z]{1,2})-([0-9]{1,}))/s', $test_string, $match);

    // destruct your array is equal to selection by index $match[$index]
    [$full_match, $match_group_1, $match_group_2, $match_group_3] = $match;

    var_dump($full_match);    // -> ["task XX-123", "task YZ-456", "task CD-789"]
    var_dump($match_group_1); // -> ["XX-123", "YZ-456", "CD-789"]
    var_dump($match_group_2); // -> ["XX", "YZ", "CD"]
    var_dump($match_group_3); // -> ["123", "456", "789"]
?>

Example: handle error

https://phpsandbox.io/n/bitter-morning-55gn

<?php

    $test_string = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy";

    preg_match_all('/(no)-(matching)-(pattern)/s', $test_string, $match);

    // get your defined match groups
    $full_match = $match[0];
    $match_group_1 = $match[1];
    $match_group_2 = $match[2];
    $match_group_3 = $match[3];

    // check if your match was successfull
    if (empty($full_match)) {
        // handle error
        print("could not match any result");
        var_dump($match);
    }
    // handle success
    else {
        print("matched something, check $match values for more details");
        var_dump($match_group_1, $match_group_2, $match_group_3);
    }

?>

See php.net docs -> https://www.php.net/manual/en/function.preg-match-all.php

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement