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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:18:48+00:00 2026-05-27T13:18:48+00:00

I set myself this rather simple sounding challenge but now I am stuck trying

  • 0

I set myself this rather simple sounding challenge but now I am stuck trying to figure out how to inject a classname onto the <body> dom element of my document.

The complexity is because I don’t have control over the HTML markup I am getting via the file_get_contents function (a third party feeds the files via FTP).

So the body element could be a multitude of different ways, for example:

<body>
<body id="my-id" data-attribute="content">
<body data-attribute="content">
<body class="already-existing-class" id="my-id" data-attribute="content">

and so on… not even the order of said attributes is under my control so you may have a class= before the id= et cetera.

I think you all understand the complexity I am talking about here; (I hope).

What I basically need is a way to use preg_replace() to inject a new class into either an existing class attribute on the body (if one already exists) or add the class attribute itself with my new class in it.

Any help would be much appreciated.

If this has already been answered, please feel free to point it out. I tried searching but with such generic terms it was hard to find what I was looking for.

Thanks for reading.

J.

  • 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-27T13:18:48+00:00Added an answer on May 27, 2026 at 1:18 pm

    A regex can be extremely cumbersome for this application. Instead, I suggest you use an HTML parser, such as PHP’s DOMDocument. Here is an example.

    $node1 = '<body>';
    $node2 = '<body id="my-id" data-attribute="content">';
    $node3 = '<body data-attribute="content">';
    $node4 = '<body class="already-existing-class" id="my-id" data-attribute="content">';
    
    foreach( range( 1, 4) as $i)
    {
        $var = 'node'.$i;
        $doc = new DOMDocument();
        $doc->loadHTML( $$var);
        foreach( $doc->getElementsByTagName( 'body') as $tag)
        {
            $tag->setAttribute('class', ($tag->hasAttribute('class') ? $tag->getAttribute('class') . ' ' : '') . 'some-new-class');
        }
        echo htmlentities( $doc->saveHTML()) . "\n";
    }
    

    Demo

    Notice the output of the <body> tag is correct. You (or another SO member) are free to determine how to extract just the body tag from the DOMDocument.

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

Sidebar

Related Questions

hey guys, i'm trying to do a rather simple select box by myself. <div
Edit: I have solved this by myself. See my answer below I have set
I find myself writing code that looks like this a lot: set<int> affected_items; while
I am trying to set myself up on a mac to learn Ruby on
I set out to get myself a neat C debugging macro, not really sure
JS Fiddle (code) This is more of a task I have set myself to
I know this could probably be done without regular expressions, but I'd rather make
i hate asking for help as i would rather figure things for myself, or
I haven't used a statically typed language in many years and have set myself
How can I set a parameter of a sub-report? I have successfully hooked myself

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.