PHP如何从DOMDocument打印文本和html标签,nodeValue只打印文本?

时间:2013-10-07 09:34:56

标签: php html echo domdocument

我正在将以下代码写入.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{
      }
    }   
}
?>

1 个答案:

答案 0 :(得分:0)

以下内容抓取<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>元素