• Social Networks:

PHP5 DOM to Search and Replace your HTML

Home » Blog » Programming & Tech » PHP5 DOM to Search and Replace your HTML

The DOM extension allows you to operate on XML documents through the DOM API with PHP 5
Here, this demo shows you the use of PHP DOM Elements to generate or replace your html as php string.
Below code contains a html string in a variable and converts it to bold as needed.

<?php
$finalHtml = "
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>

<p>Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p> 

<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p> 

<p>It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

<h2>WHAT IS LOREM IPSUM</h2>

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>

<p>Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p> 

<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p> 

<p>It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

<p>Lorem Ipsum1: Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>

<p>Lorem Ipsum2: It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>

<p>Lorem Ipsum3: It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>";

echo htmlBoldFormatting($finalHtml);

Below function converts some wanted html text in to bold formatting and its a very usefull when using direct regex or replace in html string.
For Example:
if we needs title to be bold “WHAT IS LOREM IPSUM” and some words want to convert in bold format using preg_match function in php.

<?php
function htmlBoldFormatting($yourHtml)
{
    $dom = new DOMDocument();
    libxml_use_internal_errors(true);
    @$dom->loadHTML($yourHtml, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    $xpath = new DOMXPath($dom);
    $title_flag = true;
    foreach($xpath->query('//text()') as $node) {
        $txt = trim($node->nodeValue);
        $p = $node->parentNode;
        if (preg_match("/^\s*(Lorem Ipsum1:|Lorem Ipsum2:|Lorem Ipsum3:)(.*)$/s", $node->nodeValue, $matches)) {
            // Put Choices in bold:
            $p->insertBefore($dom->createElement('strong', $matches[1]), $node);
            $node->nodeValue = " " . trim($matches[2]);
        } else 
        if (strtoupper($txt) === $txt && $txt !== '') {
            // Put title in bold.
            if($title_flag == true){
                $p->insertBefore($dom->createElement('strong', $txt), $node);
                $node->nodeValue = "";
                $title_flag = false;
            }
        }
    }
    $yourHtml = $dom->saveHTML();
    return $yourHtml;
}
?>
Get Most Usefull Share Social Media

Request a free Quote to get started

We’re a professional bunch, and we want to work with you! Talk to us today to get started.