Home >Backend Development >PHP Tutorial >How to Parse HTML in PHP without Regular Expressions?

How to Parse HTML in PHP without Regular Expressions?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-06 11:31:111000browse

How to Parse HTML in PHP without Regular Expressions?

PHP Parse HTML Code (without Regular Expressions)

You have a PHP variable containing HTML code and you need to extract the text between the headings without using regular expressions.

Solution 1: PHP Document Object Model (DOM)

This approach involves using the PHP Document Object Model (DOM) to parse the HTML:

<?php
$str = '<h1T1</h1>Lorem ipsum.<h1T2</h1>The quick red fox...<h1T3</h1>... jumps over the lazy brown FROG';
$DOM = new DOMDocument;
$DOM->loadHTML($str);

// Get all H1 elements
$items = $DOM->getElementsByTagName('h1');

// Display the text of each H1 element
for ($i = 0; $i < $items->length; $i++) {
    echo $items->item($i)->nodeValue . "<br/>";
}
?>

This will output the text within the headings:

T1
T2
T3

Solution 2: Regex Replacement (for Content Between Headings)

If the goal is to extract the content between the headings, you can use regular expressions as follows:

<?php
$str = '<h1T1</h1>Lorem ipsum.<h1T2</h1>The quick red fox...<h1T3</h1>... jumps over the lazy brown FROG';
$result = preg_replace("#<h1>.*?</h1>.*?</h1>#", "", $str);

echo $result;
?>

This will output the content between the headings:

Lorem ipsum.The quick red fox...... jumps over the lazy brown FROG

The above is the detailed content of How to Parse HTML in PHP without Regular Expressions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn