hi guys,
Do you guys have any php code where i could add a new class to a div element in a page? using my code am planning to read the file contents of an html page. I wanted to add a class "modify" to all DIVs which has a class and those DIV without a class element will have 'class="modify" ' added to it.

Thanks in advance

Have you got any PHP/HTML code that we can look at - just helps to explain your idea a bit better, and obviously also shows that you're putting some thought into what we reply with?

Member Avatar

PHP can't change the html once it's been written, so you're probably looking at javascript to do this for you.

However, as maharrington states, without seeing your code, it's impossible to say. It could be that php could indeed do this for you with just a simple conditional statement.

Here is my code....its written in an index.php file. I call the page index.php?q=index.html. This pulls up the index.html file, gets it's contents and i find all "<div " and replace it with "<div class='modify' "

The issue is <div doesn't support 2 class attributes so if there is any existing class in a div i have the append the class modify to it rather than <div class="modify" class="asdf" >

/* add class='editable' to all div elements */
$content = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $content);
$content = str_replace('<div ', '<div class="modify" ', $content);
echo $content;

Would this help? Javascript wouldn't work!

Member Avatar

Js would work!

With jQuery, you can do


THat would add a modify class to every div regardless of whether they contain other classes or not.

preg_replace is a bit of a dirty animal for this, but it could work. However, it can fail spectacularly if your html is slightly different, e.g.

<div class="someclass">  and <div class ="someclass"> and <div class= "someclass"> and <div class = "someclass">

SO you need to account for possible whitespace. Depending on the structure perhaps you could use one of the xml libraries to parse and write values. But, you may be better off getting into the guts of your index.html file, changing it to a .php file and laying some variables:

in your parent page, something like:

$mod = ($page == true) ? ' modify' : '';
$modclass = ($page == true) ? ' class="modify"' : '';

then all your include pages:

<div class="someclass<?php echo $mod;?>">

<div<?php echo $modclass;?>>

if you are using a templater, then this may be even easier:

<div class="someclass{{mod}}">


i tried js for this, It didnt work they way i wanted. Any other way around it? with PHP it worked. I think i will have to write

$content = str_replace('<div ', '<div class="modify" ', $content);

for multiple types of occurences or else i'll have to manually include the class attribute!

Member Avatar

i tried js for this, It didnt work they way i wanted.

You did include the jquery library before that line?

Member Avatar

By the way, this works fine:

<title>Untitled Document</title>



<div class="myclass">
    <p>hi again6</p>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

BE CAREFUL - jQuery 2.0.x stops supporting IE6-8, so if you need to support these, use the 1.10.x versions.