1

I have this variable with content:

$string = '<div id="pico"><figure id="attachment_84751" aria-describedby="caption-attachment-84751" style="width: 2048px" class="wp-caption aligncenter"><figcaption id="caption-attachment-84751" class="wp-caption-text">No access to a gym? No problem. Push ups and sits ups are the basis for a quick burner workout on Thanksgiving.</figcaption></figure>
<p>Happy Thanksgiving everyone! If you’re anything like us, you’re probably getting ready to put on a clinic in turkey and mashed potato consumption this afternoon — and to feel like you overdid it afterwards.</p>
<p>To help pre-empt some of that Thanksgiving feast guilt, why not get your blood flowing and your heart rate up with a quick workout before the holiday meal?</p>
<p>This five minute burner can be adjusted to any skill level.</p>
<p><strong>Here’s how it works:</strong></p>
<ul>
<li>Set a timer for five minutes. At the top of each minute, set out to do a set of push ups and sit ups. Beginners should shoot for five push ups and five sits ups per minute. Intermediate athletes should shoot for 10 of each and advanced athletes should shoot for 15 of each.</li>
<li>Once you’ve gotten through a round, rest until the next minute begins and start over.</li>
</ul>
<p>It’s just five minutes — and there will be rest in there — but you’ll definitely feel like you got a good workout in!</p>
<span style="display: inline-block; width: 2px; height: 2px;"></span></div>';

I want to get first html <figure> element content from that variable. So the result will be:

<figure id="attachment_84751" aria-describedby="caption-attachment-84751" style="width: 2048px" class="wp-caption aligncenter"><figcaption id="caption-attachment-84751" class="wp-caption-text">No access to a gym? No problem. Push ups and sits ups are the basis for a quick burner workout on Thanksgiving.</figcaption></figure>

My PHP code so far:

$output = preg_match_all('/<figure>/i', $string , $matches);
$first_figure = $matches [1] [0];

But not getting the desire result :(

xKobalt
  • 1,498
  • 2
  • 13
  • 19
Shibbir
  • 1,963
  • 2
  • 25
  • 48
  • 3
    Don't use regex for parsing HTML text. Use `DOMDocument`. – nice_dev Mar 02 '20 at 11:10
  • 1
    Agreed, use a DOM parser, see https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Wesley Smith Mar 02 '20 at 11:11
  • @vivek_23 I will use that code in `WordPress` and `DOMDocument` is working slowly. Is there any work around using `preg_match_all` or sth? – Shibbir Mar 02 '20 at 11:12
  • @kerbholz that was mistake. fixed it. – Shibbir Mar 02 '20 at 11:13
  • 1
    @creativeartbd DOMDocument would work fast. For the HTML string you shared, it should be pretty fast. – nice_dev Mar 02 '20 at 11:16
  • _“But not getting the desire result”_ - and what exactly did you expect `
    ` to match here? Your input text does not contain `
    `, and you did not use any wildcards in this “pattern” either. You are looking for a character-by-character match of the exact text `
    `, and that simply isn’t there.
    – CBroe Mar 02 '20 at 11:52

1 Answers1

1

You can use the DOMDocument class (https://www.php.net/manual/en/class.domdocument.php) like this example:

$string = '<div id="pico"><figure id="attachment_84751" aria-describedby="caption-attachment-84751" style="width: 2048px" class="wp-caption aligncenter"><figcaption id="caption-attachment-84751" class="wp-caption-text">No access to a gym? No problem. Push ups and sits ups are the basis for a quick burner workout on Thanksgiving.</figcaption></figure><p>To help pre-empt some of that Thanksgiving feast guilt, why not get your blood flowing and your heart rate up with a quick workout before the holiday meal?</p><p>This five minute burner can be adjusted to any skill level.</p><p><strong>Here’s how it works:</strong></p><ul><li>Set a timer for five minutes. At the top of each minute, set out to do a set of push ups and sit ups. Beginners should shoot for five push ups and five sits ups per minute. Intermediate athletes should shoot for 10 of each and advanced athletes should shoot for 15 of each.</li><li>Once you’ve gotten through a round, rest until the next minute begins and start over.</li></ul><p>It’s just five minutes — and there will be rest in there — but you’ll definitely feel like you got a good workout in!</p><span style="display: inline-block; width: 2px; height: 2px;"></span></div>';

// creates new instance of DOMDocument class
$dom = new domDocument;

// load the html from you variable (@ because figure will throw a warning)
@$dom->loadHTML($string);

// stores all elements of figure
$figures = $dom->getElementsByTagName('figure');

// stores the outerHTML of the first figure
$element = $dom->saveHtml($figures[0]);

// $element contains the html string of the first figure
Robin Gillitzer
  • 1,603
  • 1
  • 6
  • 17