Manipulate HTML from php - 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.

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

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>

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

Related

is this a good practice to output html using php? [duplicate]

I have been designing websites for a while now, but there is one thing that I have never been quite sure of when using PHP and HTML. Is it better to have the whole document in PHP and echo HTML like so:
<?php
doSomething();
echo "<div id=\"some_div\">Content</div>";
?>
Or have a HTML file like so and just add in the PHP:
<html>
<body>
<?php doSomething(); ?>
<div id="some_div">Content</div>
</body>
</html>
It seems tidier to echo HTML, especially if lots of PHP gets used throughout the page, but doing so loses all formatting of the HTML i.e. colors in the IDE etc.
There are varying opinions on this. I think there are two good ways:
Use a templating engine like Smarty that completely separates code and presentation.
Use your second example, but when mixing PHP into HTML, only output variables. Do all the code logic in one block before outputting anything, or a separate file. Like so:
<?php $content = doSomething();
// complex calculations
?>
<html>
<body>
<?php echo $content; ?>
<div id="some_div">Content</div>
</body>
</html>
Most full-fledged application frameworks bring their own styles of doing this; in that case, it's usually best to follow the style provided.
I think this would depend on your group's or your own decided convention. And it can and should vary depending on what type of file you're working in. If you follow the MVC pattern then your views should be the latter. If you're writing a class or some non-output script/code then you should use the former.
Try to keep a separation of display or formatting of output and the logic that provides the data. For instance let's say you need to make a quick page that runs a simple query and outputs some data. In this case (where there is no other existing infrastructure or framework) you could place the logic in an include or in the top or the bottom of the file. Example:
<?php
# define some functions here that provide data in a raw format
?>
<html>
<body>
<?php foreach($foo = data_function($some_parameter) as $key => $value): ?>
<p>
<?=$value;?>
</p>
<?php endforeach; ?>
</body>
</html>
Or you could place the logic and function definitions in an include file or at the bottom of the file.
Now if you're producing some sort of class that has output (it really shouldn't) then you would echo the HTML or return it from the method being called. Preferably return it so that it can be output whenever and however the implementer would like.
The syntax highlighting is an important benefit of the second method, as you said. But also, if you're following good practices where logic and presentation are separated, you will naturally find that your files that contain HTML are almost entirely HTML, which then, naturally, leads to your second method again. This is the standard for MVC frameworks and the like. You'll have a bunch of files that are all PHP, doing logic, and then when that's done they'll include a presentation file which is mostly HTML with a sprinkling of PHP.
Simple:
More PHP - close HTML in PHP. When you generate HTML code in PHP, when you are doing something like a class, so it is better to make it in echo.
Less PHP - close PHP in HTML. This is stuff like just putting vars into fields of HTML stuff, like forms... And such.
The best approach is to separate the HTML from the PHP using template system or at least some kind of HTML skeleton like:
<main>
<header/>
<top-nav/>
<left-col>
<body />
</left-col>
<right-col />
<footer/>
</main>
Each node represents a template file e.g. main.php, hrader.php and so on. Than you have to separate the PHP code from the templates as something like functions.php and fineally use your second approach for template files and keeping functions clean of "echos" and HTML.
If you can, use a template engine instead.
Although it is slightly easier at first to mix your HTML and PHP, separating them makes things much easier to maintain later on.
I would recommend checking out TemplateLite which is based on Smarty but is a little more light weight.
I've reached a conclusion that using views in MVC framework e.g. Laravel, Yii, CodeIgniter is the best approach even if you are not displaying the html straight away.
Inside the view do all the echoing and looping of prepared variables, don't create or call functions there, unless formatting existing data e.g. date to specific format date('Y-m-d', strtodate(123456789)). It should be used only for creating HTML, not processing it. That's what frameworks have controllers for.
If using plain PHP, create you own view function to pass 3 variables to - html file, array of variables, and if you want to get output as string or print it straight away for the browser. I don't find a need for it as using frameworks is pretty much a standard. (I might improve the answer in the future by creating the function to get view generated HTML) Please see added edit below as a sample.
Frameworks allow you to get the HTML of the view instead of displaying it. So if you need to generate separate tables or other elements, pass the variables to a view, and return HTML.
Different fremeworks may use various type of templating languages e.g. blade. They help formatting the data, and essentially make templates easier to work with. It's also not necessary to use them for displaying data, or if forced to use it by the framework, just do required processing before posting the variables, and just "print" it using something like {{ yourVariable }} or {{ yourVariable.someProperty }}
Edit: here's a plain PHP (not framework PHP) - simple-php-view repository as a sample view library that allows to generate HTML using variables. Could be suitable for school/university projects or such where frameworks may not be allowed.
The repository allows to generate HTML at any time by calling a function and passing required variables to it, similar to frameworks. Separately generated HTML can then be combined by another view.
It depends on the context. If you are outputting a lot of HTML with attributes, you're going to get sick of escaping the quotation marks in PHP strings. However, there is no need to use ?><p><? instead of echo "<p>"; either. It's really just a matter of personal taste.
The second method is what I usually use. And it was the default method for me too. It is just to handy to get php to work inside html rather than echo out the html code. But I had to modify the httpd.conf file as my server just commented out the php code.

What is the best practice to use when using PHP and HTML?

I have been designing websites for a while now, but there is one thing that I have never been quite sure of when using PHP and HTML. Is it better to have the whole document in PHP and echo HTML like so:
<?php
doSomething();
echo "<div id=\"some_div\">Content</div>";
?>
Or have a HTML file like so and just add in the PHP:
<html>
<body>
<?php doSomething(); ?>
<div id="some_div">Content</div>
</body>
</html>
It seems tidier to echo HTML, especially if lots of PHP gets used throughout the page, but doing so loses all formatting of the HTML i.e. colors in the IDE etc.
There are varying opinions on this. I think there are two good ways:
Use a templating engine like Smarty that completely separates code and presentation.
Use your second example, but when mixing PHP into HTML, only output variables. Do all the code logic in one block before outputting anything, or a separate file. Like so:
<?php $content = doSomething();
// complex calculations
?>
<html>
<body>
<?php echo $content; ?>
<div id="some_div">Content</div>
</body>
</html>
Most full-fledged application frameworks bring their own styles of doing this; in that case, it's usually best to follow the style provided.
I think this would depend on your group's or your own decided convention. And it can and should vary depending on what type of file you're working in. If you follow the MVC pattern then your views should be the latter. If you're writing a class or some non-output script/code then you should use the former.
Try to keep a separation of display or formatting of output and the logic that provides the data. For instance let's say you need to make a quick page that runs a simple query and outputs some data. In this case (where there is no other existing infrastructure or framework) you could place the logic in an include or in the top or the bottom of the file. Example:
<?php
# define some functions here that provide data in a raw format
?>
<html>
<body>
<?php foreach($foo = data_function($some_parameter) as $key => $value): ?>
<p>
<?=$value;?>
</p>
<?php endforeach; ?>
</body>
</html>
Or you could place the logic and function definitions in an include file or at the bottom of the file.
Now if you're producing some sort of class that has output (it really shouldn't) then you would echo the HTML or return it from the method being called. Preferably return it so that it can be output whenever and however the implementer would like.
The syntax highlighting is an important benefit of the second method, as you said. But also, if you're following good practices where logic and presentation are separated, you will naturally find that your files that contain HTML are almost entirely HTML, which then, naturally, leads to your second method again. This is the standard for MVC frameworks and the like. You'll have a bunch of files that are all PHP, doing logic, and then when that's done they'll include a presentation file which is mostly HTML with a sprinkling of PHP.
Simple:
More PHP - close HTML in PHP. When you generate HTML code in PHP, when you are doing something like a class, so it is better to make it in echo.
Less PHP - close PHP in HTML. This is stuff like just putting vars into fields of HTML stuff, like forms... And such.
The best approach is to separate the HTML from the PHP using template system or at least some kind of HTML skeleton like:
<main>
<header/>
<top-nav/>
<left-col>
<body />
</left-col>
<right-col />
<footer/>
</main>
Each node represents a template file e.g. main.php, hrader.php and so on. Than you have to separate the PHP code from the templates as something like functions.php and fineally use your second approach for template files and keeping functions clean of "echos" and HTML.
If you can, use a template engine instead.
Although it is slightly easier at first to mix your HTML and PHP, separating them makes things much easier to maintain later on.
I would recommend checking out TemplateLite which is based on Smarty but is a little more light weight.
I've reached a conclusion that using views in MVC framework e.g. Laravel, Yii, CodeIgniter is the best approach even if you are not displaying the html straight away.
Inside the view do all the echoing and looping of prepared variables, don't create or call functions there, unless formatting existing data e.g. date to specific format date('Y-m-d', strtodate(123456789)). It should be used only for creating HTML, not processing it. That's what frameworks have controllers for.
If using plain PHP, create you own view function to pass 3 variables to - html file, array of variables, and if you want to get output as string or print it straight away for the browser. I don't find a need for it as using frameworks is pretty much a standard. (I might improve the answer in the future by creating the function to get view generated HTML) Please see added edit below as a sample.
Frameworks allow you to get the HTML of the view instead of displaying it. So if you need to generate separate tables or other elements, pass the variables to a view, and return HTML.
Different fremeworks may use various type of templating languages e.g. blade. They help formatting the data, and essentially make templates easier to work with. It's also not necessary to use them for displaying data, or if forced to use it by the framework, just do required processing before posting the variables, and just "print" it using something like {{ yourVariable }} or {{ yourVariable.someProperty }}
Edit: here's a plain PHP (not framework PHP) - simple-php-view repository as a sample view library that allows to generate HTML using variables. Could be suitable for school/university projects or such where frameworks may not be allowed.
The repository allows to generate HTML at any time by calling a function and passing required variables to it, similar to frameworks. Separately generated HTML can then be combined by another view.
It depends on the context. If you are outputting a lot of HTML with attributes, you're going to get sick of escaping the quotation marks in PHP strings. However, there is no need to use ?><p><? instead of echo "<p>"; either. It's really just a matter of personal taste.
The second method is what I usually use. And it was the default method for me too. It is just to handy to get php to work inside html rather than echo out the html code. But I had to modify the httpd.conf file as my server just commented out the php code.

php dom problem

hello I'm new to PHP programming and I migrated from ASP .net to PHP..
I have a div just like below
<div id="mydiv"></div>
what I wanted to do is just to change the text and html content(like some name or any data in it) in it.
What I imagine is just like
mydiv=>innertext="some value";
Thanks,
GURU
Are you trying to modify the div in the same page as the php? I don't think that's possible.
PHP runs at the server, and only sees content between the php tags.
If you're modifying dom content, it seems like javascript/jQuery is a better approach for this.
Otherwise, if you're modifying content that is hosted already, and plan output it to a different page, then you can use this:
PHP Simple HTML DOM Parser
$myDivText = "This is what goes inside mydiv";
echo "<div id=\"mydiv\">$myDivText</div>";

PHP: Loading up an HTML file without it tidying my code

I am using the loadhtml function (http://php.net/manual/en/domdocument.loadhtml.phpt) to load up an external .html file. When I load it, it "tidy's" up my code, which, I don't want. I do NOT want a full HTML document, I only want html snippets in my .html, and I don't want the loadhtml file to try to make it valid html, because I don't want it to.
Is there a better function to load up a .html file so that it does not tidy up the code?!
If you want to just put the HTML into a string, you can just use:
$file1 = file_get_contents("file.html");
LoadHTML puts your html file data into a DOM structure.
If it is not really HTML (snippets aren't) then you can't really make a DOM structure -- but that is what you asked for.
Now, the first question comment asked what you wanted to do once this data was loaded, so either you have a good answer for that which will point us in a new direction, or the answer is simply NO.
To store:
$contents = file_get_contents('example.html');
To output:
readfile('example.html');

DOM class injection in PHP

idea
Via jQuery, I was able to mark all :first-child and :last-child elements in document (well, almost all :)) with class first which could I later style (i.e. first li in ul#navigation would be easily adressable as ul#navigation .first).
I used following code:
var $f = $('*:first-child')
$f.addClass('first');
var $l = $('body *:last-child')
$l.addClass('last');
example
http://jsbin.com/ikuca/3
Example is already here - it's not the way to do it however, it's just an idea prototyped in other, for me at the moment easier language.
question
Now, my question is if it's possible to do the same via php, so non-JS users/gadgets could have the same effects and additional styling and also it would be less overkill on browser.
So, is it possible to capture output, parse it as html and inject this class easily in php?
clarification
I'm quite aware of output buffering, just haven't done much stuff with it - also, i'm not sure about modificating output string in php as parsed dom (without regex) - and how tough on server it'll be - with caching of course, so this whole stuff will run once until the page will be edited again.
I'm sure you could use output buffering to capture your assembled PHP page and then use DOM and XPath on it to add the class attributes, but the question is, why don't you just put the classes onto the elements when assembling the page in the first place? Saves you the jQuery and the capturing.
Also, adding the CSS classes with jQuery to be able to do ul#navigation.first is somewhat odd too, because the jQuery expression you used is a CSS selector, so you could use it directly to style the first child from your CSS file. The only reason to add a class .first is if you want to be backwards compatible with browsers unable to process :first-child.
I think you'll find it's easier to do this in jQuery than PHP, but it can be done in PHP.
To capture output you want output buffering, which you activate with the ob_start function, before sending any output. You can pass ob_start() a PHP function which will receive the HTML code as a parameter and can then manipulate the HTML using PHP's DOM functions.
jQuery runs in the client's borwser. PHP runs on the server. You can't modify the DOM in the browser from the server once it is served.
What you could do, is to serve the page already with the proper classes. For example in PHP when you print a table:
<table>
<?php
$i=0;
foreach ($rows as $row):
?>
<tr class=<?php echo ($i%2==0?'even':'odd')?>
<td><?php echo $row;</td>
</tr>
<?php
endforeach;
?>
</table>
ps. Do you really want to support clients without JS?
You could even use the same CSS selectors by using some of the libraries mentioned here.
I read that the phpQuery library even has the same :first-child pseudo-class you need.
I sincerely hope you plan on using caching or else your CPU usage will go up 100% with a few request.

Categories