[英]DOMDocument stripping tags from inline scripts PHP
This is a strange one but looks like $dom->saveHTML() is stripping tags from inline javascript这是一个奇怪的,但看起来 $dom->saveHTML() 正在从内联 javascript 中剥离标签
$domStr = '
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>my page</title>
<script>
var elem = "<div>some content</div>";
</script>
</head>
<body>
<div>
MY PAGE
</div>
</body>
</html>
';
$doc = new DOMDocument();
libxml_use_internal_errors(true);//prevents tags in js from throwing errors; see php.net manual
$doc->formatOutput = true;
$doc->strictErrorChecking = false;
$doc->preserveWhiteSpace = true;
$doc->loadHTML($domStr);
echo $doc->saveHTML();
exit;
http://sandbox.onlinephpfunctions.com/code/ad59a2a1016b2128e437ef61dbe00f1c511bff8d http://sandbox.onlinephpfunctions.com/code/ad59a2a1016b2128e437ef61dbe00f1c511bff8d
if you use libxml_use_internal_errors(true);如果您使用 libxml_use_internal_errors(true); you will not see what is wrong but if removed you get你不会看到什么是错的,但如果删除你得到
<b>Warning</b>: DOMDocument::loadHTML(): Unexpected end tag : div
Same thing happens with同样的事情发生在
$doc->formatOutput = false;
Any help is appreciated.任何帮助表示赞赏。
I've avoided this by not including any HTML in my inline JavaScript. 通过在嵌入式JavaScript中不包含任何HTML,避免了这种情况。 Instead, I've added <template>
elements containing the HTML string I want to manipulate in JS, and then I read that dynamically at runtime. 相反,我添加了<template>
元素,其中包含要在JS中处理的HTML字符串,然后在运行时动态读取该元素。 For example: 例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>my page</title>
</head>
<body>
<div>
MY PAGE
</div>
<template id="content-template">
<div>some content</div>
</template>
<script>
var elem = document.getElementById('content-template').innerHTML;
...
</script>
</body>
</html>
It is probably a bug of DomDocument.这可能是 DomDocument 的一个错误。
You have to escape the closing tag of HTML in JS or it gets misinterpreted.你必须在 JS 中转义 HTML 的结束标记,否则它会被误解。
This should work var elem = "<div>some content<\/div>";
这应该可以工作var elem = "<div>some content<\/div>";
Alternatively, if you pass option 1 to the loadHtml the parser will ignore it.或者,如果您将选项 1 传递给 loadHtml,解析器将忽略它。
In a bit of an oddity 1 can mean both LIBXML_SCHEMA_CREATE and LIBXML_ERR_WARNING as these two predefined constants have the same value.有点奇怪的是,1 可能意味着 LIBXML_SCHEMA_CREATE 和 LIBXML_ERR_WARNING,因为这两个预定义常量具有相同的值。 Presumably it is meant to be LIBXML_SCHEMA_CREATE which does the following "Create default/fixed value nodes during XSD schema validation".据推测,它应该是 LIBXML_SCHEMA_CREATE,它执行以下“在 XSD 模式验证期间创建默认/固定值节点”。
在DOCTYPE
声明之后,您丢失了<html>
开头的标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.