使用 php Dom 在 div 中获取 php 标签

get php tags inside a div with php Dom

我正在尝试使用 php Dom 通过 id 获取元素获取 div 中的值,当我尝试这样做时,我得到的值是 ' games and新游戏从 div 发布,但我根本没有得到 php 值和 php 标签,例如。 < ? php echo hellow world 如下所示。我想获取变量 $existing_Data 中 div 的所有内容,包括 < ? php 和 < h1 > 等..

$selected_div = "div_footer";
$file = '../myfolder/22selected.php';
$file_get = file_get_contents($file);

/* // which one to use here ? i am getting a headache !
$file_entity = htmlentities($file_get);
$file_htmlcharac = htmlspecialchars($file_entity);
$file_strip = stripslashes($file_htmlcharac);
*/

$doc = new DOMdocument();
$doc->loadHTML($file_get);
$element = $doc->getElementById($selected_div);
$existing_Data = $element->firstChild->nodeValue;

echo '<table>';
echo '<form action= "existing_Data.php" method = "POST">';
echo 'Text Content <textarea rows="12" cols="60" name="content" id ="text_content">' . $existing_Data . '</textarea>';
echo '<input type ="submit" name = "submit" id = "submit" value = "Submit" ></form>';

// div_footer CONTENTS 
<div id = "div_footer">
<h1> All games</h1>
<p> new games releases</p>
<?php echo "hello world"; ?>
</div>

从你展示的内容,我们无法猜测你想要实现什么。

当您使用 file_get_contents() 打开一个没有方案(http、ftp、..)的文件时,PHP 会尝试通过本地文件系统打开文件,因此标签没有被 PHP 解释。缺点是,它无效 XML/HTML,因此 DOM 解析器无济于事。

您应该将数据写入数据库或至少写入 XML、JSON 或纯文本文件。要取回值,您需要做的就是 $value = trim(file_get_contents('../mydata.txt'));.

另一种情况是,您想要 (http-)POST 该数据。那么也没有理由使用 DOM 解析器。只需将数据放入隐藏字段 <input type="hidden" name="data" value="<?php echo $data; ?>.

要防止不同(并非所有)类型的 XSS,请参阅

htmlentities() vs. htmlspecialchars()

How to prevent XSS with HTML/PHP?

我强烈建议阅读此 QA:

What are the differences between server-side and client-side programming?