Manipulate HTML from php
Asked Answered
H

3

6

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.

Hennery answered 13/12, 2010 at 9:10 Comment(4)
Does this php file come from another website? Or put it this way: is your code and that div located in two different files?Libbielibbna
Why don't you use PHP to generate the HTML page?Mitra
(related) Best Methods to parse HTMLBootlick
possible duplicate of Replace the content of a tag with a certain classBootlick
S
19

You can use PHP's DOM classes/functions to do this.

Start by creating/loading your document:

$d = new DOMDocument();
$d->loadHTML($yourWellFormedHTMLString);

Then you'll want to locate the document node that you want to alter. You can do this using XPath:

$xpathsearch = new DOMXPath($d);
$nodes = $xpathsearch->query('//div[contains(@class,'main')]');  

Then you'll want to iterate over matching nodes, and create new nodes inside:

foreach($nodes as $node) {
    $newnode = $d->createDocumentFragment();
    $newnode->appendXML($yourCodeYouWantToFillIn);
    $node->appendChild($newnode);
}

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 selector div.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. :)

Stoa answered 13/12, 2010 at 10:14 Comment(2)
loadHtml (unlike loadXml) does not require $yourWellFormedHTMLString. It can parse real world broken HTML.Bootlick
just as note to this solution, if anyone stumbles upon it: as mentioned by @weston-c the query is not going to only return those elements which have class "main", but rather all elements whose class-names contain "main". so class="maintain" with also be returned. instead, if you use //div[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')], you can make the querying more precise. It's also a bit easier than the solution presented in the answer's link... hope this helps.Epicedium
S
1

If it just needs to include a static fragment

<div class="main">
<?php readfile ('path/to/some/file'); ?>
</div>

If it needs to include the output of another PHP script

<div class="main">
<?php include ('path/to/some/file') ?>
</div>
Spartacus answered 13/12, 2010 at 9:54 Comment(1)
it won't be a php file to include. It will be a static html file.Hennery
E
0

You read the file with:

$fileContents=file_get_contents($file_path);

http://php.net/manual/en/function.file-get-contents.php

Then you search and replace the div content:

$newHtmlContent=preg_replace("/<div class=\"main\">(.*)</div>/i",'<div class="main">Some text here</div>',$fileContents);

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:

file_put_contents($file_path,$newHtmlContent);

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

Ensoul answered 13/12, 2010 at 9:28 Comment(6)
Will... this work if there if there is 2 class <div class="main class2">Hennery
You can sometimes use regular expressions to good effect, but they're often clumsy tools for rewriting tree-ish structures like markup documents. It's usually worth considering a real markup parser instead.Stoa
It depends, the regular expression approach can bee drastically improoved and could work verry well in your case, I don't have the time to digg up a better expression right now. In that case, if there are 2 class names it wouldn't work with that exact regular expression. Try using the html parser...Ensoul
this has to flexible so that i can use it with different html file with different classes.Hennery
Then, I would recommend trying to use the provided HTML parser, I have bolded it in the answer..Ensoul
Regex and html are a bad idea. #1732848Ratiocinate

© 2022 - 2024 — McMap. All rights reserved.