html_entity_decode() 和一些括号转换使我能够显示 HTML 代码 <textarea></textarea> within <textarea> 块。
这是我在这里的第一次回复,所以请多多关照。 :)
我必须修复一个 PHP 文本文件编辑器,它会在 <textarea></textarea> 窗口中输出其文件内容 -- 但是,如果它读取的文件 包含 <textarea></textarea> 块,则输出窗口将关闭块并将其解释为 HTML,关闭 </textarea> 和文件的其余部分将不会显示在编辑器窗口中。
(换句话说,它是一个 PHP 脚本,它加载文件并将其内容显示在 <textarea></textarea> 窗口内以进行编辑等)
即包含的文件:
Enter stuff here: <textarea name="blah"></textarea> Yay Stuff.
它显示为:
Enter stuff here: <textarea name="blah">(缺少所有其他内容!)
需要两种类型的解释。
对于 PHP 文件:
在殴打自己并在此站点的帮助下,我能够通过首先将结尾 </textarea> 转换为 &lt/textarea&gt 来“清理”块来欺骗解释器。
例如:
($Body 只是要在编辑器中显示的文件内容)
//if reading a <textarea block of code
if (preg_match("%<textarea%/i",$Body)) {
//turn < > into < > for the end of textarea block
$Body=preg_replace("%</textarea>%/i","</textarea>", $Body);
//sanitize (this is the output window where the file content goes)
print html_entity_decode("</font><textarea>" .$Body. "</textarea>\n");
}
这使得窗口可以正确输出文件内容如:
Enter stuff here: <textarea name="blah"></textarea> 好东西。
对于 非 PHP 文件(如 HTML):
你必须搜索</textarea>并且可以使用".$Body."
即
$filetype=pathinfo($File);
$filetype['extension'];
$php_files=Array('php','php3','php5');
if (!in_array($filetype['extension'], $php_files)) {
$Body=preg_replace("%</textarea>%/i","</textarea>", $Body);
echo "</font><textarea> ".$Body." </textarea>\n";
}
注意:如果 PHP 文件不包含<textarea>,则可以使用:
print html_entity_decode($Body) 我也不得不为此奋斗。
注2:
我正在显示 PHP 5+ 代码,但我必须为 PHP 5 执行此操作,仅供参考,它适用于 eregi() 和 eregi_replace()
这让我发疯了,所以我希望这对其他人有帮助。
......太疯狂了,我再次回来并更新了我的答案。 :)