php dom problem - php

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>";

Related

PHP + Smarty: Parse PHP+HTML into a String?

I am using PHP in combination with Smarty Templates to generate pages serverside. Currently, I am loading a page as follows:
$smarty->assign('app', file_get_contents("some_content.php"));
Where some content contains HTML with PHP tags and code inside those tags.
I would like the PHP content inside this file within the current scope (That of the script reading the file), so that a particular function I've defined is available. How would I go about doing so? All the information I can find is regarding the eval(...) function, which doesn't seem to be able to cope with the HTML/PHP mixture: would I need to perform a find/eval/replace operation to achieve the desired result, or is there a more elegant way of doing this?
From my opinion, this short snippet of the code you posted shows that something is generally wrong there :)
But nevertheless you can achieve whatever you are trying to achieve by doing the following:
ob_start();
include("some_content.php");
$result = ob_get_clean();
$smarty->assign('app', $result);
Ich, I'm such a dummkopf. There is an answer right on the PHP manual for eval, right under my nose. Here is the answer I neglected to notice.
You can use {literal}...{/literal} smarty tags to display any content in smarty templates as is. It used to transfer java scripts and other specific content.

PHP library to create inlined CSS from files or from <style> tags?

I would like a library to create inlined CSS+HTML from .html and .css files or from an HTML file with tags in the head. I would prefer a PHP library if possible.
By inlined CSS I mean something like
<span style="font: bla bla bla">Hi There!</span>
versus
<style>
.greeting { font: bla bla bla; }
</style>
I often need to put HTML into emails and this would simplify the process greatly.
If anyone is interested, my current solution (for stuff that isn't restyled often) is to use the Smarty templating engine to create the document, and to assign the style="" part to a variable inside the template.
Then I can use that variable in each tag (like <td {$td_style}> (FYI - {$variable} is how smarty variables are inserted into a template) and have it generate the appropriate email-friendly HTML.
However I want something that is more general, and for which I can just feed it some HTML and CSS rather than have to convert all of it to a smarty template.
Does anyone know if a library like this exists?
Although I don't know of a library that handles this, if you're set on PHP, you could do this fairly easily I would think by using CSSTidy to parse the CSS, and then using an XML parser (ideal if your code is well-formed--maybe SimpleXML or the DOM extension) or one of the PHP-based HTML parsers I see out there to parse the HTML, so you can alter the HTML via DOM methods according to CSS rules.
If you don't need it tied into a PHP process though, I recommend going with JavaScript for almost anything like this, since it can work in the client-side (and in different environments) and be a handy tool for anyone wishing to do this; you should be able to parse the CSS using http://glazman.org/JSCSSP/ and then using DOMParser() and the IE equivalent as needed (for XHTML) or use innerHTML, this htmlparser, or any other HTML Parsers.
Btw, I know <style> works in emails since my add-on Color Source injects them to get syntax highlighting in emails, though I guess you were only saying it is more complicated for you to do it that way.

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

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.

How can I include PHP in a vBulletin style?

I'd like to use PHP in a vBulletin style/skin, yet when I do so, the PHP doesn't work.
By 'not work', I mean it's treated just as plain text. And if you look in the code you can see the PHP code (hidden, by Firefox - which is the behaviour you get if you put PHP code on a plain html page)
http://vapurl.com/h0kp3fqr8c - view source, and you'll see what I mean.
You can't stick random PHP into the vBulletin templating system; it gives you some simple branching options in the form of
<if condition="$somevariable === $someothervariable">
some text or HTML to be displayed
</else>
some other text or HTML to be displayed
</if>
A good explanation of how the template conditional system works can be found in the vBulletin manual here.
Your short url appears to have expired, but if the code you wish to include is more complicated than that, you need to start looking into vBulletin hooks and plugins.
Assuming you're entering the correct PHP syntax, this may be occurring if you're trying to use any of the standard output methods.
If you are trying to use echo, print or any other similar output function, try wrapping all of your included PHP within ob functions.
Example:
ob_start();
//PHP code I want to run goes here
echo "Test this works?";
ob_end_clean();
hooks /plugins are the best way of manipulating data before displaying it in the template. You can add as much php as you want in a plugin and then modify the template to output the results.

Categories