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 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 513k
  • Answers 513k
  • 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 There are several useful toString() and deepToString() methods in java.util.Arrays… May 16, 2026 at 6:02 pm
  • Editorial Team
    Editorial Team added an answer An update to resolve this will be applied this weekend.… May 16, 2026 at 6:02 pm
  • Editorial Team
    Editorial Team added an answer I second that you'll see differences only when you have… May 16, 2026 at 6:02 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'm looking for a suitable cross-platform web framework (if that's the proper term). I
I’m looking for a suitable distribution of Subversion for deployment within an enterprise. It
I am looking for a suitable package (free or otherwise) to duplicate the functionality
I was looking for some suitable 2D element container. What I want is the
Can someone give a head-to-head comparison between them? We are looking for a suitable
As the title states, I am looking for something, that will help me automate
Looking at posts like this and others, it seems that the correct way to
Looking on-line I saw that I can write most of the application in Ansi-C
Looking at several posts, I get a feel that many of the questions arise
Looking to pass variables from c# to javascript to use some jquery code. Passing

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.