In JavaScript in innerHTML I can enter tags. In PHP DOM it i
Moderator: General Moderators
In JavaScript in innerHTML I can enter tags. In PHP DOM it i
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
Last edited by McInfo on Thu Jun 17, 2010 2:04 pm, edited 1 time in total.
In PHP5+ in PHP DOM
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?
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: In JavaScript in innerHTML I can enter tags. In PHP DOM it i
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.
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
It is possible to work with an HTML document using DOM...
...but the usual way is to create HTML is to simply output it directly.
Edit: This post was recovered from search engine cache.
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();Code: Select all
<html>
<head>
<title><?php echo 'Page Title'; ?></title>
</head>
<body>
<p><?php echo 'Paragraph in the Body'; ?></p>
</body>
</html>I'm not sure what that means. Can you give an example?sneta wrote:Can I insert html tags into text tags that won't be replaced into plain text?
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 2:05 pm, edited 1 time in total.
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: In JavaScript in innerHTML I can enter tags. In PHP DOM it i
Whoops, its apparent I misunderstood what he was asking.McInfo wrote:It is possible to work with an HTML document using DOM...
Using PHP DOMDoc,DOMElement etc is EXACTLY what I mean. Howe
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>');
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
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.
Edit: This post was recovered from search engine cache.