dongpobo6009 2016-07-11 11:01
浏览 130
已采纳

php html标签转换为字符串

I am trying to process a HTML file with php as a DOM document. Processing is okay, but when I save the html document with $html->saveHTMLFile("file_out.html"); all link tags are 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 the links as php scripts, maybe this makes a difference? I cannot convert the &lt; back to < with htmlentitites_decode() or such. Is there any other conversion or encoding I can use?

The php script looks like the 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 as $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 as $child) {
    $element->removeChild($child);
  }
  $element->appendChild($node);
}

setInnerHTML($html, $elem, $text);
$html->saveHTMLFile($ref.".html");
header('Location: '."tracking.php?ref=$ref&user=unLock");
?>

We get the reference to a file from "id" and "ref" and the input data from array "textareaX". Next I open the file, identify the html element by id and replace its content (a link) with the input data from the textarea. I provide only the href in the textarea and the script builds the hyperlink from that. Next I plug this back into the original file and overwrite the input file.

When I write the new file though, the link <a href= ...> </a> is converted to &lt;a href=...&gt; &lt;/a&gt;, which is a problem.

  • 写回答

1条回答 默认 最新

  • douyuan4697 2016-07-11 16:29
    关注

    Here is part of your code with the issue identified:

    <?php
    
    function setInnerHTML($DOM, $element, $innerHTML) {
      /*********************************
          Well, there's your problem:
      **********************************/
      $node = $DOM->createTextNode($innerHTML);
      $children = $element->childNodes;
      foreach ($children as $child) {
        $element->removeChild($child);
      }
      $element->appendChild($node);
    }
    
    ?>
    

    What you are doing is passing your new anchor (a) tag as a string then creating a text node out of it (text is just that - text, not HTML). The createTextNode function automatically encodes any HTML tags so that they will be visible as text when viewed by a browser (this is so you can present HTML as visible code on your page if you choose to).

    What you need to do is create the element as HTML (not a text node) then append it:

    <?php
    
    function setInnerHTML($DOM, $element, $innerHTML) {
    
      $f = $DOM->createDocumentFragment();
      $f->appendXML($innerHTML);
      $element->appendChild($f);
    
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!