Page 1 of 1
In JavaScript in innerHTML I can enter tags. In PHP DOM it i
Posted: Wed Nov 11, 2009 4:50 am
by sneta
s xlated to entitites and entered as text. Is there a similiar mechanism in PHP? If not what do I do if I want to enter designed code? (by "designed" I mean with tags like <b><i> etc)
Re: In JavaScript in innerHTML I can enter tags. In PHP DOM
Posted: Wed Nov 11, 2009 11:44 am
by McInfo
PHP Manual:
What is PHP?
Edit: This post was recovered from search engine cache.
In PHP5+ in PHP DOM
Posted: Wed Nov 11, 2009 12:47 pm
by sneta
Can I insert html tags into text tags that won't be replaced into plain text? In JavaScript there's innerHTML which allows that. Does PHP have something similar?
Re: In JavaScript in innerHTML I can enter tags. In PHP DOM it i
Posted: Wed Nov 11, 2009 12:54 pm
by flying_circus
What McInfo is trying to communicate is that PHP doesnt have a DOM as you are asking. PHP cannot interact with rendered HTML code, as PHP is processed on the server BEFORE any HTML is sent to a browser and rendered.
You can use PHP to inject "designed code" but after it is sent to the client, PHP is done.
As for the link that McInfo posted, he is saying "RTFM to better understand exactly what PHP IS and DOES", because you havent got a grasp on that concept yet.
Re: In JavaScript in innerHTML I can enter tags. In PHP DOM
Posted: Wed Nov 11, 2009 1:43 pm
by McInfo
It is possible to work with an HTML document using
DOM...
Code: Select all
<?php
$html = new DOMDocument();
$html->loadHTML('<html><head><title></title></head><body></body></html>');
$html->getElementsByTagName('title')->item(0)->appendChild($html->createTextNode('Page Title'));
$p = $html->createElement('p', 'Paragraph in the Body');
$html->getElementsByTagName('body')->item(0)->appendChild($p);
echo $html->saveHTML();
...but the usual way is to create HTML is to simply output it directly.
Code: Select all
<html>
<head>
<title><?php echo 'Page Title'; ?></title>
</head>
<body>
<p><?php echo 'Paragraph in the Body'; ?></p>
</body>
</html>
sneta wrote:Can I insert html tags into text tags that won't be replaced into plain text?
I'm not sure what that means. Can you give an example?
Edit: This post was recovered from search engine cache.
Re: In JavaScript in innerHTML I can enter tags. In PHP DOM it i
Posted: Wed Nov 11, 2009 2:05 pm
by flying_circus
McInfo wrote:It is possible to work with an HTML document using
DOM...
Whoops, its apparent I misunderstood what he was asking.

Using PHP DOMDoc,DOMElement etc is EXACTLY what I mean. Howe
Posted: Wed Nov 11, 2009 3:34 pm
by sneta
When I create say a <div> and I want to enter <b>bold</b> the problem is that "<" is xlated to entity "<" while i want to enter a real tag. An additional complication is that I get the html to "inject" into the div from outside so I can't know that in advance and I have to add to HTML as is.
$d = $html->createElement('div', '<b>bold</b>');
Posted: Wed Nov 11, 2009 3:56 pm
by sneta
won't work. the tags are xformed to entities "<" turns to "<" or plain text. To make things more complicated I get the HTML that I have to enter the div from outside so I can't know in advance what it is and I have to put it all in. A simple javascript innerHTML is working in client side but how do I solve it in server side?
Re: In JavaScript in innerHTML I can enter tags. In PHP DOM
Posted: Wed Nov 11, 2009 4:40 pm
by McInfo
Import the HTML into the current DOMDocument with DOMDocument::createDocumentFragment() and
DOMDocumentFragment::appendXML(), then use DOMNode::appendChild() to place the fragment where you need it.
Edit: This post was recovered from search engine cache.