php html tags converted to string -


i trying process html file php dom document. processing okay, when save html document $html->savehtmlfile("file_out.html"); link tags converted from:

click here: <a title="editable" href="http://somewhere.net">somewhere.net</a> 

to

click here: &lt;a title="editable" href="http://somewhere.net"&gt; somewhere.net &lt;/a&gt;  

i process links php scripts, maybe makes difference? cannot convert &lt; < htmlentitites_decode() or such. there other conversion or encoding can use?

the php script looks following:

<?php $text = $_post["textareax"]; $id = $_get["id"]; $ref = $_get["ref"]; $html = new domdocument();  $html->preservewhitespace = true; $html->formatoutput       = false; $html->substituteentities = false; $html->loadhtmlfile($ref.".html");  $elem = $html->getelementbyid($id);  $elem->nodevalue = $innerhtml;  if ($text == "")   { $text = "--- no details. ---"; } $newtext = ""; $words = explode(" ",$text); foreach ($words $word) {   if (strpos($word, "http://") !== false) {     $newtext .= "<a alt=\"editable\" href=\"".$word."\">".$word."</a>";      }   else {$newtext .= $word." ";} }  $text = $newtext;  function setinnerhtml($dom, $element, $innerhtml) {   $node = $dom->createtextnode($innerhtml);   $children = $element->childnodes;   foreach ($children $child) {     $element->removechild($child);   }   $element->appendchild($node); }  setinnerhtml($html, $elem, $text); $html->savehtmlfile($ref.".html"); header('location: '."tracking.php?ref=$ref&user=unlock"); ?> 

we reference file "id" , "ref" , input data array "textareax". next open file, identify html element id , replace content (a link) input data textarea. provide href in textarea , script builds hyperlink that. next plug original file , overwrite input file.

when write new file though, link <a href= ...> </a> converted &lt;a href=...&gt; &lt;/a&gt;, problem.

here part of code issue identified:

<?php  function setinnerhtml($dom, $element, $innerhtml) {   /*********************************       well, there's problem:   **********************************/   $node = $dom->createtextnode($innerhtml);   $children = $element->childnodes;   foreach ($children $child) {     $element->removechild($child);   }   $element->appendchild($node); }  ?> 

what doing passing new anchor (a) tag string creating text node out of (text - text, not html). createtextnode function automatically encodes html tags visible text when viewed browser (this can present html visible code on page if choose to).

what need create element html (not text node) append it:

<?php  function setinnerhtml($dom, $element, $innerhtml) {    $f = $dom->createdocumentfragment();   $f->appendxml($innerhtml);   $element->appendchild($f);  }  ?> 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -