I'm having an html file, index.php
I want to take the content within a <div>
with the class main
of that file and replace it with another text. How can i achieve that?
Sample content in html:
<div class="main">
Replace this text with some code!
</div>
I want get the content within this div using php and replace it with another content. But I have no idea on how to do this.
Update: I'm aware of client side trick with javascript. I want to do this server side. And the file will be html and not php. so I think i have to open the html in php and do this, though i don't precisely how.
Can this be done with xpath or html dom parser or something? A google search gave me these terms but i have no clue what they actually are.
You read the file with:
http://php.net/manual/en/function.file-get-contents.php
Then you search and replace the div content:
http://php.net/manual/en/function.preg-replace.php
My regular expression is a little rusty, but you can scoop it up in here: http://www.regular-expressions.info/tutorial.html
Then save the new content:
http://www.php.net/manual/en/function.file-put-contents.php
Or you could parse the file using this: http://simplehtmldom.sourceforge.net/ But it must be well formed.
I would recommend this version as the above will fail if the contet of the main div is another div...
You can use PHP's DOM classes/functions to do this.
Start by creating/loading your document:
Then you'll want to locate the document node that you want to alter. You can do this using XPath:
Then you'll want to iterate over matching nodes, and create new nodes inside:
If you don't mind messing around with a library at an early stage of development, take a look at CAST (content-addressed style templating). It's pretty much designed to do what you're describing, and if nothing else, you could peer inside the source to see examples.
(NOTE: I'm sure the astute will note that
//div[contains(@class,'main')]
isn't exactly the equivalent of the CSS selectordiv.main
... since the class attribute can contain more than one class. Doing this precisely is unwieldy enough I think it's better to start with the simplified expression when you're introducing people to it, even if it might best for those who go this route to eventually get to know xpath well enough to handle this right. Or, just use ids more instead of classes. :)If it just needs to include a static fragment
If it needs to include the output of another PHP script