Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulate HTML from php

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.

like image 713
esafwan Avatar asked Dec 13 '10 09:12

esafwan


People also ask

Can you edit HTML in PHP?

php files by default. So if you want to edit an HTML file, like footer. html, you'll need to copy it over to the child theme before making any changes using a file transfer protocol (FTP) tool or your hosting provider's control panel.

Can you do DOM manipulation in PHP?

So if you're ever working with the content for a post (a post type or a custom post type, for that matter) and you need to manipulate tags much like you would with JavaScript, then using the DomDocument library is one of the most powerful tools are your disposal.

How can I use HTML in PHP?

Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to use the PHP. Step 2: Now, we have to place the cursor in any tag of the <body> tag where we want to add the code of PHP. And, then we have to type the start and end tag of PHP.


1 Answers

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

like image 125
Weston C Avatar answered Oct 03 '22 19:10

Weston C