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

  • Home
  • SEARCH
  • 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 900805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:27:01+00:00 2026-05-15T15:27:01+00:00

I have an html page loaded into a PHP variable and am using str_replace

  • 0

I have an html page loaded into a PHP variable and am using str_replace to change certain words with other words. The only problem is that if one of these words appears in an important peice of code then the whole thing falls to bits.

Is there any way to only apply the str_replace function to certain html tags? Particularly: p,h1,h2,h3,h4,h5

EDIT:

The bit of code that matters:

 $yay = str_ireplace($find, $replace , $html); 

cheers and thanks in advance for any answers.

EDIT – FURTHER CLARIFICATION:

$find and $replace are arrays containing words to be found and replaced (respectively). $html is the string containing all the html code.

a good example of it falling to bits would be if I were to find and replace a word that occured in e.g. the domain name. So if I wanted to replace the word ‘hat’ with ‘cheese’. Any occurance of an absolute path like

http://www.worldofhat.com/images/monkey.jpg
would be replaced with:
http://www.worldofcheese.com/images/monkey.jpg

So if the replacements could only occur in certain tags, this could be avoided.

  • 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-05-15T15:27:01+00:00Added an answer on May 15, 2026 at 3:27 pm

    Do not treat the HTML document as a mere string. Like you already noticed, tags/elements (and how they are nested) have meaning in an HTML page and thus, you want to use a tool that knows what to make of an HTML document. This would be DOM then:

    Here is an example. First some HTML to work with

    $html = <<< HTML
    <body>
        <h1>Germany reached the semi finals!!!</h1>
        <h2>Germany reached the semi finals!!!</h2>
        <h3>Germany reached the semi finals!!!</h3>
        <h4>Germany reached the semi finals!!!</h4>
        <h5>Germany reached the semi finals!!!</h5>
        <p>Fans in Germany are totally excited over their team's 4:0 win today</p>
    </body>
    HTML;
    

    And here is the actual code you would need to make Argentina happy

    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    $nodes = $xpath->query('//*[self::h1 or self::h2 or self::p]');
    foreach( $nodes as $node ) {
        $node->nodeValue = str_replace('Germany', 'Argentina', $node->nodeValue);
    }
    echo $dom->saveHTML();
    

    Just add the tags you want to replace content in the XPath query call. An alternative to using XPath would be to use DOMDocument::getElementsByTagName, which you might know from JavaScript:

     $nodes = $dom->getElementsByTagName('h1');
    

    In fact, if you know it from JavaScript, you might know a lot more of it, because DOM is actually a language agnostic API defined by the W3C and implemented in many languages. The advantage of XPath over getElementsByTagName is obviously that you can query multiple nodes in one go. The drawback is, you have to know XPath 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a html page on my localhost - get_description.html . The snippet below
C# Visual Studio 2010 I am loading a complex html page into a webbrowser
I'm trying to parse some HTML with DOM in PHP, but I'm having some
I am working with ASP.NET MVC and have a view page with a simple
I have a page that has a series of "related" selects. Everything works fine
Before you answer this I have never developed anything popular enough to attain high
For many years I have had an objective of moving out of ASP/VBScript to
I have a few selects that I have built as external components in a
I have this very simple script that allows the user to specify the url
I'm in a curious situation where I previously had no problem achieving what I'm

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.