2013-10-07 105 views
1

我正在將下面的代碼寫入.html文件。我喜歡讀出正在工作的值。 但我只能看到你可以在「輸出」處看到的文字。我需要在<td>標籤內打印html標籤!PHP如何從DOMDocument,nodeValue而不是文本打印文本和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{ 
     } 
    } 
} 
?> 

回答

0

以下graps到HTML內部<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"; 
} 

注意,在循環$i步驟,2跳過頭。您應該將標頭更改爲<th>元素

相關問題