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

Ask A Question

Stats

  • Questions 476k
  • Answers 476k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I found that the WikiNotes example project from Google does… May 16, 2026 at 4:56 am
  • Editorial Team
    Editorial Team added an answer In Tools | Options | Environment | Keyboard enter "Word"… May 16, 2026 at 4:56 am
  • Editorial Team
    Editorial Team added an answer You need get all entites of Product with category computer… May 16, 2026 at 4:56 am

Trending Tags

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

Top Members

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.