PHP 如何从 DOMDocument 打印文本和 html 标签,nodeValue 而不是仅文本


PHP How to print text AND html tags from DOMDocument, nodeValue instead only text?

我正在将以下代码写入.html文件。我喜欢读出有效的值。但我只得到您在"输出"中看到的文本。我也需要在<td>标签内打印 html 标签!

以下是浏览器打印的内容:

hoehere Spruenge
2x 2x 2x 2x 
Adrenalin-Modus (Zeitlupe, staerkere Schlaege) (**)
2x 
maximaler Sex-Appeal
2x 3x 

这就是我不会得到的:

hoehere Spruenge 
2x <img src="images/cheats/psx/o.gif"> 2x <img src="images/cheats/psx/d.gif"> 2x <img src="images/cheats/psx/o.gif"> <img src="images/cheats/psx/l.gif"> <img src="images/cheats/psx/r.gif"> <img src="images/cheats/psx/ve.gif"> 2x <img src="images/cheats/psx/r2.gif">

等等...

data.html 文件中的代码:

<TABLE BORDER CELLPADDING="2">
  <TR>
    <TD>höhere Sprünge</TD>
    <TD>2x <img src="images/cheats/psx/o.gif"> 2x <img src="images/cheats/psx/d.gif"> 2x <img src="images/cheats/psx/o.gif"> <img src="images/cheats/psx/l.gif"> <img src="images/cheats/psx/r.gif"> <img src="images/cheats/psx/ve.gif"> 2x <img src="images/cheats/psx/r2.gif"></TD>
  </TR>
  <TR>
    <TD>Adrenalin-Modus (Zeitlupe, stärkere Schläge) (**)</TD>
    <TD>2x <img src="images/cheats/psx/k.gif"> <img src="images/cheats/psx/ve.gif"> <img src="images/cheats/psx/r1.gif"> <img src="images/cheats/psx/l1.gif"> <img src="images/cheats/psx/k.gif"> <img src="images/cheats/psx/u.gif"> <img src="images/cheats/psx/l.gif"> <img src="images/cheats/psx/k.gif"></TD>
  </TR>
  <TR>
    <TD>maximaler Sex-Appeal</TD>
    <TD><img src="images/cheats/psx/ks.gif"> 2x <img src="images/cheats/psx/d.gif"> <img src="images/cheats/psx/o.gif"> <img src="images/cheats/psx/ks.gif"> <img src="images/cheats/psx/r1.gif"> <img src="images/cheats/psx/l2.gif"> <img src="images/cheats/psx/o.gif"> <img src="images/cheats/psx/d.gif"> 3x <img src="images/cheats/psx/l1.gif"></TD>
  </TR>
  <TR>
</TABLE>

PHP代码:

<?php
// array for JSON response
$response = array();
include("functions.php");
// check for required fields
if (isset($_POST['CHEAT_VALUE'])) {
    $htmltext = $_POST['CHEAT_VALUE'];
    $gameid = $_POST['GAME_ID'];
    $howto = $_POST['CHEAT_HOWTO'];
    $plattform = $_POST['PLATTFORM_ID'];
    $myFile = "data.html";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $htmltext;
    fwrite($fh, $stringData);
    fclose($fh);
            require_once 'db_connect.php';
        // connecting to db
        $db = new DB_CONNECT();
      // new dom object
  $dom = new DOMDocument();
  //load the html
  $html = $dom->loadHTMLFile("data.html");
  //discard white space 
  $dom->preserveWhiteSpace = false; 
  //the table by its tag name
  $tables = $dom->getElementsByTagName('table'); 
  //get all rows from the table
  $rows = $tables->item(0)->getElementsByTagName('tr'); 
  // loop over the table rows
  foreach ($rows as $row) 
  { 
   // get each column by tag name
      $cols = $row->getElementsByTagName('td'); 
   // echo the values  
      if($cols->item(1)->nodeValue && $cols->item(0)->nodeValue){
            $datetime = date("Y-m-d");
                // mysql inserting a new row
                $col1 = $cols->item(0)->nodeValue;
                $col2 = $cols->item(1)->nodeValue;              
                $col1 = ereg_replace ("ä", "ae", $col1); 
                $col1 = ereg_replace ("ö", "oe", $col1); 
                $col1 = ereg_replace ("ü", "ue", $col1); 
                $col1 = ereg_replace ("ß", "ss", $col1); 
                $col1 = ereg_replace ("Ä", "Ae", $col1); 
                $col1 = ereg_replace ("Ö", "Oe", $col1); 
                $col1 = ereg_replace ("Ü", "Ue", $col1);
                $col2 = ereg_replace ("ä", "ae", $col2); 
                $col2 = ereg_replace ("ö", "oe", $col2); 
                $col2 = ereg_replace ("ü", "ue", $col2); 
                $col2 = ereg_replace ("ß", "ss", $col2); 
                $col2 = ereg_replace ("Ä", "Ae", $col2); 
                $col2 = ereg_replace ("Ö", "Oe", $col2); 
                $col2 = ereg_replace ("Ü", "Ue", $col2);    
                echo $col1;
                echo "<br>";
                echo $col2;
      }else{
      }
    }   
}
?>
下面抓取

<td>里面的html:

$str = '
<TABLE BORDER CELLPADDING="2">
  <TR>
    <TD>höhere Sprünge</TD>
    <TD>2x <img src="images/cheats/psx/o.gif"> 2x <img src="images/cheats/psx/d.gif"> 2x <img src="images/cheats/psx/o.gif"> <img src="images/cheats/psx/l.gif"> <img src="images/cheats/psx/r.gif"> <img src="images/cheats/psx/ve.gif"> 2x <img src="images/cheats/psx/r2.gif"></TD>
  </TR>
  <TR>
    <TD>Adrenalin-Modus (Zeitlupe, stärkere Schläge) (**)</TD>
    <TD>2x <img src="images/cheats/psx/k.gif"> <img src="images/cheats/psx/ve.gif"> <img src="images/cheats/psx/r1.gif"> <img src="images/cheats/psx/l1.gif"> <img src="images/cheats/psx/k.gif"> <img src="images/cheats/psx/u.gif"> <img src="images/cheats/psx/l.gif"> <img src="images/cheats/psx/k.gif"></TD>
  </TR>
  <TR>
    <TD>maximaler Sex-Appeal</TD>
    <TD><img src="images/cheats/psx/ks.gif"> 2x <img src="images/cheats/psx/d.gif"> <img src="images/cheats/psx/o.gif"> <img src="images/cheats/psx/ks.gif"> <img src="images/cheats/psx/r1.gif"> <img src="images/cheats/psx/l2.gif"> <img src="images/cheats/psx/o.gif"> <img src="images/cheats/psx/d.gif"> 3x <img src="images/cheats/psx/l1.gif"></TD>
  </TR>
  <TR>
</TABLE>
';
$doc = new DOMDocument();
$doc->loadHTML($str);
$tds = $doc->getElementsByTagName('td');
$doc->loadHTML($str);
//header("Content-type: text/plain");
$len = $tds->length;
for($i = 1; $i < $len; $i += 2){
    echo $doc->saveXML($tds->item($i)) . "'n";
}

循环中注意$i步骤,2跳过标头。 您应该将标题更改为<th>元素