简体   繁体   中英

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

I am writing the following code to a .html file. I like to read out the values which is working. But I only get the text like you can see at "output". I need to print the html tags inside the <td> tags too!

Here is what the browser prints out:

hoehere Spruenge
2x 2x 2x 2x 

Adrenalin-Modus (Zeitlupe, staerkere Schlaege) (**)
2x 

maximaler Sex-Appeal
2x 3x 

and this is what I wont to get:

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">

and so on...

code inside the data.html file:

<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 Code:

<?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{
      }
    }   
}
?>

the following graps the html inside <td> :

$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";
}

Note in loop $i steps by 2 to skip headers. you should change the headers to <th> elements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM