How to get specific html element from string using PHP

creativeartbd :

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 :(

Robin Gillitzer :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=220190&siteId=1