Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:46:34+00:00 2026-06-12T08:46:34+00:00

We are looking, the script that can easily replace the value using PHP DOM.

  • 0

We are looking, the script that can easily replace the value using PHP DOM.
here we have a HTML code that i need to replace

HTML Code

<html>
<head>
<head>
<body>
<div> Explore HISTORY shows, watch videos and full episodes, play games and access articles on historical topics at History.com <p>Miss an episode of your favorite History shows? Go to history.com to catch up on full episodes and video exclusives.</p></div>
<div>Discover what happened today in history. Read about major past events that happened today including special entries on crime, entertainment, and more.</div>
<p>Experience games from your favorite shows, take quizzes, solve puzzles and more!</p>
</body>
</html>

we have to replace the word ‘history'(including bold/small char.) with <u>history</u>

the final code would be

<html>
<head>
<head>
<body>
<div> Explore <u>HISTORY</u> shows, watch videos and full episodes, play games and access articles on historical topics at <u>History</u>.com <p>Miss an episode of your favorite <u>History</u> shows? Go to <u>history</u>.com to catch up on full episodes and video exclusives.</p></div>
<div>Discover what happened today in <u>history</u>. Read about major past events that happened today including special entries on crime, entertainment, and more.</div>
<p>Experience games from your favorite shows, take quizzes, solve puzzles and more!</p>
</body>
</html>

This is what I have tried, but it does not work:

<?php
libxml_use_internal_errors(true);
@$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->loadHTMLFile('http://www.history.com');
@$body = $doc->getElementsByTagName('body');
       $i=0;
 while(is_object($finance = $doc->getElementsByTagName("body")->item($i)))
             {
                      foreach($finance->childNodes as $nodename)
                      {
                          $node = $doc->createElement("para", "<u>as fasd fasd fadsf</u>");
                          if(stristr($nodename->nodeValue, 'search')){
                          $nodename->appendChild($node);
                          echo $nodename->getAttribute."<br>" ;
                          echo $nodename->nodeValue."<br>" ;
                          @$us = true;
                          }
                        echo $nodename->nodeValue."<br>" ;
                        }

       $i++;
             }
libxml_clear_errors();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:46:35+00:00Added an answer on June 12, 2026 at 8:46 am

    Use DOMXPath to find nodes that contain the word “history” case insensitive, then split it into new text nodes.

    I went ahead and wrote an implementation of this, out of curiosity. It took me longer than I planned, but it definitely works. I hope it is helpful to you.

    <?php
    
    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = FALSE;
    $doc->resolveExternals = FALSE;
    $doc->loadHTML(<<<END
    <html>
    <head>
    </head>
    <body>
    <div> Explore HISTORY shows, watch videos and full episodes, play games and access articles on historical topics at History.com <p>Miss an episode of your favorite History shows? Go to history.com to catch up on full episodes and video exclusives.</p></div>
    <div>Discover what happened today in history. Read about major past events that happened today including special entries on crime, entertainment, and more.</div>
    <p>Experience games from your favorite shows, take quizzes, solve puzzles and more!</p>
    </body>
    </html>
    END
    );
    
    echo '<p>Input:</p>'."\n";
    echo $doc->saveHTML()."\n";
    
    $word    = 'history';
    $lcWord  = strtolower($word);
    $wordLen = strlen($word);
    $xpath   = new DOMXPath($doc);
    $nodes   = $xpath->query('/html/body//text()['.
                               'contains('.
                                  'translate(.,"'.strtoupper($word).'","'.$lcWord.'"),'.
                                  '"'.$lcWord.'")'.
                             ']');
    foreach ($nodes as $node)
    {
    // Split all occurances of "word" into new text nodes.
        $text    = $node->data;
        $textPos = 0;
        while (($wordPos = stripos($text,$word)) !== FALSE)
        {
            $beforeText = substr($text,$textPos,$wordPos - $textPos);
            $wordText   = substr($text,$wordPos,$wordLen);
    
        // Add the before text to the DOM.
            $node->parentNode->insertBefore($doc->createTextNode($beforeText),$node);
    
        // Add the word text to the DOM.
        // Underline this word.
            $uNode = $doc->createElement('u');
            $uNode->appendChild($doc->createTextNode($wordText));
            $node->parentNode->insertBefore($uNode,$node);
    
        // Repeat for the text after the word.
            $text = substr($text,$wordPos + $wordLen);
        }
    
    // Create a text node for text following the word.
        if ($text)
            $node->parentNode->insertBefore($doc->createTextNode($text),$node);
    
    // Remove the original text node.
        $node->parentNode->removeChild($node);
    }
    
    echo '<p>Output:</p>'."\n";
    echo $doc->saveHTML()."\n";
    
    ?>
    

    OUTPUT

    <p>Input:</p>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    <html>
    <head></head>
    <body>
    <div> Explore HISTORY shows, watch videos and full episodes, play games and access articles on historical topics at History.com <p>Miss an episode of your favorite History shows? Go to history.com to catch up on full episodes and video exclusives.</p>
    </div>
    <div>Discover what happened today in history. Read about major past events that happened today including special entries on crime, entertainment, and more.</div>
    <p>Experience games from your favorite shows, take quizzes, solve puzzles and more!</p>
    </body>
    </html>
    
    <p>Output:</p>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    <html>
    <head></head>
    <body>
    <div> Explore <u>HISTORY</u> shows, watch videos and full episodes, play games and access articles on historical topics at <u>History</u>.com <p>Miss an episode of your favorite <u>History</u> shows? Go to <u>history</u>.com to catch up on full episodes and video exclusives.</p>
    </div>
    <div>Discover what happened today in <u>history</u>. Read about major past events that happened today including special entries on crime, entertainment, and more.</div>
    <p>Experience games from your favorite shows, take quizzes, solve puzzles and more!</p>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a PHP script that can be run as a cron job
I'm looking for a script (or one that I can easily modify to do
I'm looking for a script/tool that can be customized to check and enforce coding/naming
I am looking for a helper tool/ script that can be used to power
I have been looking for a recursive checkout/checkin script, and everything that I have
I looking for a script that will enable me to select an area in
Im looking for a script that will take OData feed and download some .wmv
I'm looking for a script that will generate jpg thumbnails on the fly (in
I'm looking for a very simple script that will tell me if one twitter
I'm looking for some direction on a script that I'd like to build. Essentially,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.