0

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

3
Contributors
7
Replies
42
Views
4 Years
Discussion Span
Last Post by diafol
0

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?

0

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.

0

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" >

<?php
if(isset($_GET['q']))
{
$content=file_get_contents($_GET['q']);
/* 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!

1

Js would work!

With jQuery, you can do

$('div').addClass("modify");

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}}">

<div{{$modclass}}>
0
$('div').addClass("modify");

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!

0

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

You did include the jquery library before that line?

0

By the way, this works fine:

<!DOCTYPE HTML>
<html>
<head>
<title>Untitled Document</title>
<style>
    .modify{
        color:blue;
    }

</style>
</head>

<body>

<div>
    <p>hi6</p>
</div>
<div class="myclass">
    <p>hi again6</p>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
    $('div').addClass("modify");
</script>
</body>
</html>

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

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.