Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get first HTML element from a string

I was reading this article. This function that it includes:

<?php 
    function getFirstPara($string){
        $string = substr($string,0, strpos($string, "</p>")+4);
        return $string;
    }
?>

...seems to return first found <p> in the string. But, how could I get the first HTML element (p, a, div, ...) in the string (kind of :first-child in CSS).

like image 643
Toni Michel Caubet Avatar asked Mar 22 '23 10:03

Toni Michel Caubet


2 Answers

It's generally recommended to avoid string parsing methods to interrogate html.

You'll find that html comes with so many edge cases and parsing quirks that however clever you think you've been with your code, html will come along and whack you over the head with a string that breaks your tests.

I would highly recommend you use a php dom parsing library (free and often included by default with php installs).

For example DomDocument:

$dom = new \DOMDocument;
$dom->loadHTML('<p>One</p><p>Two</p><p>Three</p>');
$elements = $dom->getElementsByTagName('body')->item(0)->childNodes;

print '<pre>';
var_dump($elements->item(0));
like image 170
Harry B Avatar answered Apr 01 '23 18:04

Harry B


You could use http://php.net/strstr as the article

first search for "<p>" this will give you the full string from the first occurrence and to the end

$first = strstr($html, '<p>');

then search for "</p>" in that result, this will give you all the html you dont want to keep

$second = strstr($first, '</p>');

then remove the unwanted html

$final = str_replace($second, "", $first);

The same methode could be use to get the first child by looking for "<" and "</$" in the result from before. You will need to check the first char/word after the < to find the right end tag.

like image 26
Lohardt Avatar answered Apr 01 '23 16:04

Lohardt