1

I need to write a php file that I can 'include' in a web page. When I include the file on any page, I need it to parse the HTML in the page and add a new CSS class to all img elements in the page.

The question is, my friends, is this at all possible? Bear in mind, I want to be able to do this to many pages by simply including my php file.

Thanks!!!

1
  • 3
    You'd be better of using JavaScript Commented Jul 8, 2011 at 23:50

3 Answers 3

5

Does this have to be done with PHP on the server side? Because it's easier to accomplish with Javascript on the client side, since they HTML is already formed. You can include this block of javascript

// Plain javascript example
function addImgClass() {
  var images = document.getElementsByTagName("img");
  var numImg = images.length;
  for (var i=0; i<numImg; i++) {
    images[i].className = images[i].className + " " + "your-new-CSS-class";
  }
}

<!-- add to your html -->
<body onload="addImgClass();">
 etc. etc.

Or if you are already using jQuery, it's even easier still. Other Javascript frameworks can accomplish the same with slightly different syntax if you happen to have Dojo or Prototype available for use.

// jQuery example
$(document).ready(function() {
  $("img").addClass("your-new-CSS-class");
});
Sign up to request clarification or add additional context in comments.

Comments

1

Yes and no.

No, because you cannot simply include a php file in your page and have it process other content... it is not possible as the HTML content will not be processed by PHP, but output directly.

Yes, you can do that indirectly: e.g. load the HTML into a DOM document in PHP, modify it, and output the resulting structure.

However, as mentioned by Michael, this is better done client-side by Javascript... with the advantage that you will work on the final, formed HTML; the (only) disadvantage being that if JS is disabled (very improbable these days, though) it will not work.

3 Comments

Thanks guys. I was hoping to avoid JavaScript as I also want to eventually add paragraph text and I believe search robots wouldn't see this content as they don't run JavaScript. Hence wanted to alter the HTML on the server side using the include.
Note you can add text server-side and still add the class client-side.
Cool. Can I get the included php script to update the text in any element of the calling file (the one that is importing it)?
0

This is easily possible, and one of the great things about PHP. One thing to bear in mind, however, is that if the file you're including another file into is a .html file, it won't be parsed by PHP so the include won't be found; make sure all your files are .php, with the appropriate headers set for each, if it's a CSS file say.

As @Michael has pointed out in the comments, you can tell the PHP parser to look through .html files, however it's better practice to only get it to parse native .php files.

7 Comments

The .html files can be parsed by your web server as PHP if you put the appropriate file extension handler in the directory that contains them. It is a little odd to do so though.
Wah!! They last guys said it couldn't be done. Are you sure it's possible? So I have a file called Home.php which includes HTML plus an include statement to a file called update.php. The code in update php then updates the HTML in Home.php?
Ah. It won't update the HTML in Home.php - it will only include more code from update.php into Home.php. If you want to modify the HTML in Home.php, you either print it out with PHP, read the file with PHP from another one which in the latter case is the wrong way to do it. Could you edit your question to include the specific HTML, and what you want to change?
Sorry, why is that wrong? Isn't that how templates work (OK, templates abstract away more stuff and avoid repetition, but still)?
The OP is asking about changing the content of one file simply by including another in it. I do agree that templates rock, but from how I understood the original question, it's not (easily, cleanly) possible to do what the OP is asking :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.