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 855021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:00:57+00:00 2026-05-15T08:00:57+00:00

I am looking for suitable replacement code that allows me replace the content inside

  • 0

I am looking for suitable replacement code that allows me replace the content inside of any HTML tag that has a certain class e.g.

$class = "blah";
$content = "new content";
$html = '<div class="blah">hello world</div>';

// code to replace, $html now looks like:
// <div class="blah">new content</div>

Bare in mind that:

  1. It wont necessarily be a div, it could be <h2 class="blah">
  2. The class can have more than one class and still needs to be replaced e.g. <div class="foo blah green">hello world</div>

I am thinking regular expressions should be able to do this, if not I am open to other suggestions such as using the DOM class (although I would rather avoid this if possible because it has to be PHP4 compatible).

  • 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-15T08:00:58+00:00Added an answer on May 15, 2026 at 8:00 am

    Do not use regular expressions to parse HTML. You can use the built in DOMDocument, or something like simple_html_dom:

    require_once("simple_html_dom.php");
    
    $class = "blah";
    $content = "new content";
    $html = '<div class="blah">hello world</div>';
    
    $doc = new simple_html_dom();
    $doc->load($html);
    
    foreach ( $doc->find("." . $class) as $node ) {
        $node->innertext = $content;
    }
    

    Sorry, I didn’t see the PHP4 requirement. Here’s a solution using the standard DOMDocument as mentioned above.

    function DOM_getElementByClassName($referenceNode, $className, $index=false) {
        $className = strtolower($className);
        $response  = array();
    
        foreach ( $referenceNode->getElementsByTagName("*") as $node ) {
            $nodeClass = strtolower($node->getAttribute("class"));
    
            if (
                    $nodeClass == $className || 
                    preg_match("/\b" . $className . "\b/", $nodeClass)
                ) {
                $response[] = $node;
            }
        }
    
        if ( $index !== false ) {
            return isset($response[$index]) ? $response[$index] : false;
        }
    
        return $response;
    }
    
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    
    foreach ( DOM_getElementByClassName($doc, $class) as $node ) {
        $node->nodeValue = $content;
    }
    
    echo $doc->saveHTML();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for suitable .NET class that allows me to store a large number
I'm looking for a Python library that's suitable, with DOM access too. I don't
I'm looking for a suitable client side architecture that will meet some or all
I'm looking for a key/value store in python that would be suitable for holding
I'm looking for a higher-level system language, if possible, suitable for formal verification, that
I'm looking for a Sphinx charset_table that is suitable for natural language almost all
I'm looking for a suitable cross-platform web framework (if that's the proper term). I
I'm looking for a good open source message bus that is suitable for embedded
I'm looking for interesting programming puzzles, problems or challenges suitable for a class of
I’m looking for a suitable distribution of Subversion for deployment within an enterprise. It

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.