Is there a way I can insert <
or >
in HTML file using PHP. Here is a a part of code
有没有一种方法可以用PHP在HTML文件中插入 <或> 。这是代码的一部分。
<?php
$custom_tag = $searchNode->nodeValue = "<";
$custom_tag = $searchNode->setAttribute("data-mark", $theme_name);
$custom_tag = $dom->saveHTML($searchNode);
$root = $searchNode->parentNode;
$root->removeChild($searchNode);
$test = $dom->createTextNode("<?php echo '$custom_tag'; ?>");
$root->appendChild($test);
$saved_file_in_HTML = $dom->saveHTML();
$saved_file = htmlspecialchars_decode($saved_file_in_HTML);
file_put_contents("test.html", $saved_file);
The problem is that I get <
using above method and I would like to have <
.
问题是我得到了;使用上述方法,我希望有<。
EDIT: Full code:
编辑:完整代码:
if($searchNode->hasAttribute('data-put_page_content')) {
$custom_tag = $searchNode->nodeValue = "'; \$up_level = null; \$path_to_file = 'DATABASE/PAGES/' . basename(\$_SERVER['PHP_SELF'], '.php') . '.txt'; for(\$x = 0; \$x < 1; ) { if(is_file(\$up_level.\$path_to_file)) { \$x = 1; include(\$up_level.\$path_to_file); } else if(!is_file(\$up_level.\$path_to_file)) { \$up_level .= '../'; } }; echo '";
$custom_tag = $searchNode->setAttribute("data-mark", $theme_name);
$custom_tag = $dom->saveHTML($searchNode);
$root = $searchNode->parentNode;
$root->removeChild($searchNode);
$test = $dom->createTextNode("<?php echo '$custom_tag'; ?>");
$root->appendChild($test);
$saved_file_in_HTML = $dom->saveHTML();
$saved_file = htmlspecialchars_decode($saved_file_in_HTML);
file_put_contents("../THEMES/ACTIVE/". $theme_name ."/" . $trimed_file, $saved_file);
copy("../THEMES/ACTIVE/" . $theme_name . "/structure/index.php", "../THEMES/ACTIVE/" . $theme_name ."/index.php");
header("Location: editor.php");
}
FINAL EDIT: If you want to have >
or <
using PHP DOMs methods, it is working using createTextNode()
method.
最后编辑:如果您想要使用>或 <使用php dom方法,它可以使用createtextnode()方法。< p>
2 个解决方案
#1
0
The original question appears to concern how to use PHP to manufacture an HTML tag, so I'll address that question. While it is a good idea to use htmlentities(), you also need to be aware that it's primary purpose is to protect your code so that a malicious user doesn't inject it with javascript or other tags that could create security problems. In this case, the only way to generate HTML is to create HTML and PHP provides at least three ways to accomplish this feat.
最初的问题似乎涉及到如何使用PHP来制造一个HTML标记,所以我将回答这个问题。虽然使用htmlentities()是一个好主意,但您还需要知道,它的主要目的是保护代码,以免恶意用户使用javascript或其他可能产生安全问题的标记来注入代码。在这种情况下,生成HTML的唯一方法是创建HTML, PHP至少提供了三种方法来完成这一任务。
You may write code such as the following:
您可以编写如下代码:
<?php
define("LF_AB","<");
define("RT_AB",">");
echo LF_AB,"div",RT_AB,"\n";
echo "content\n";
echo LF_AB,"/div",RT_AB,"\n";
The code produces start and end div tags with some minimal content. Note, the defines are optional, i.e. one could code in a more straightforward fashion <?php echo "<"; ?>
instead of resorting to using a define() to generate the left-angle tag character.
该代码生成具有少量内容的开始和结束div标记。注意,定义是可选的,例如,可以用更直接的方式编写代码 而不是使用define()生成左角标记字符。
However, if one is wary of generating HTML in this manner, then you may also use html_entity_decode:
但是,如果您担心以这种方式生成HTML,那么您也可以使用html_entity_decode:
<?php
define("LF_AB",html_entity_decode("<"));
define("RT_AB",html_entity_decode(">"));
echo LF_AB,"div",RT_AB,"\n";
echo "content\n";
echo LF_AB,"/div",RT_AB,"\n";
This example treats the arguments to html_entity_decode as harmless HTML entities and then converts them into HTML left and right angle characters respectively.
这个示例将html_entity_decode的参数视为无害的HTML实体,然后分别将它们转换为HTML左角和右角字符。
Alternately, you could also take advantage of the DOM with PHP, even with an existing HTML page, as follows:
另外,您还可以利用DOM使用PHP,甚至使用现有的HTML页面,如下所示:
<?php
function displayContent()
{
$dom = new DOMDocument();
$element = $dom->createElement('div', 'My great content');
$dom->appendChild($element);
echo $dom->saveHTML();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<style>
div {
background:#ccbbcc;
color:#009;
font-weight:bold;
}
</style>
</head>
<body>
<?php displayContent(); ?>
</body>
</html>
Note: the Dom method createTextNode true to its name creates text and not HTML, i.e. the browser will only interpret the right and left angle characters as text without any additional meaning.
注意:Dom方法createTextNode只创建文本,而不是HTML,也就是说浏览器只会将左右角字符解释为文本,没有任何附加的含义。
#2
-1
If I understand you correctly, I think you could use < and > in the first place. They should be rendered as HTML. Might save you some coding. Let me know if it works.
如果我理解正确,我认为您可以首先使用 <和> 。它们应该被呈现为HTML。可以为您节省一些代码。如果有用,请告诉我。
#1
0
The original question appears to concern how to use PHP to manufacture an HTML tag, so I'll address that question. While it is a good idea to use htmlentities(), you also need to be aware that it's primary purpose is to protect your code so that a malicious user doesn't inject it with javascript or other tags that could create security problems. In this case, the only way to generate HTML is to create HTML and PHP provides at least three ways to accomplish this feat.
最初的问题似乎涉及到如何使用PHP来制造一个HTML标记,所以我将回答这个问题。虽然使用htmlentities()是一个好主意,但您还需要知道,它的主要目的是保护代码,以免恶意用户使用javascript或其他可能产生安全问题的标记来注入代码。在这种情况下,生成HTML的唯一方法是创建HTML, PHP至少提供了三种方法来完成这一任务。
You may write code such as the following:
您可以编写如下代码:
<?php
define("LF_AB","<");
define("RT_AB",">");
echo LF_AB,"div",RT_AB,"\n";
echo "content\n";
echo LF_AB,"/div",RT_AB,"\n";
The code produces start and end div tags with some minimal content. Note, the defines are optional, i.e. one could code in a more straightforward fashion <?php echo "<"; ?>
instead of resorting to using a define() to generate the left-angle tag character.
该代码生成具有少量内容的开始和结束div标记。注意,定义是可选的,例如,可以用更直接的方式编写代码 而不是使用define()生成左角标记字符。
However, if one is wary of generating HTML in this manner, then you may also use html_entity_decode:
但是,如果您担心以这种方式生成HTML,那么您也可以使用html_entity_decode:
<?php
define("LF_AB",html_entity_decode("<"));
define("RT_AB",html_entity_decode(">"));
echo LF_AB,"div",RT_AB,"\n";
echo "content\n";
echo LF_AB,"/div",RT_AB,"\n";
This example treats the arguments to html_entity_decode as harmless HTML entities and then converts them into HTML left and right angle characters respectively.
这个示例将html_entity_decode的参数视为无害的HTML实体,然后分别将它们转换为HTML左角和右角字符。
Alternately, you could also take advantage of the DOM with PHP, even with an existing HTML page, as follows:
另外,您还可以利用DOM使用PHP,甚至使用现有的HTML页面,如下所示:
<?php
function displayContent()
{
$dom = new DOMDocument();
$element = $dom->createElement('div', 'My great content');
$dom->appendChild($element);
echo $dom->saveHTML();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<style>
div {
background:#ccbbcc;
color:#009;
font-weight:bold;
}
</style>
</head>
<body>
<?php displayContent(); ?>
</body>
</html>
Note: the Dom method createTextNode true to its name creates text and not HTML, i.e. the browser will only interpret the right and left angle characters as text without any additional meaning.
注意:Dom方法createTextNode只创建文本,而不是HTML,也就是说浏览器只会将左右角字符解释为文本,没有任何附加的含义。
#2
-1
If I understand you correctly, I think you could use < and > in the first place. They should be rendered as HTML. Might save you some coding. Let me know if it works.
如果我理解正确,我认为您可以首先使用 <和> 。它们应该被呈现为HTML。可以为您节省一些代码。如果有用,请告诉我。