我有一个 HTML 代码,其中有一些 JS 库使用的诸如@click、之类的属性。@autocomplete:change
当我使用 DOMDocument 解析 HTML 时,这些属性将被删除。
示例代码:
<?php
$content = <<<'EOT'
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head></head>
<body>
<a role="tab" @click="activeType=listingType"></a>
<input type="text" @autocomplete:change="handleAutocomplete">
</body>
</html>
EOT;
// creating new document
$doc = new DOMDocument('1.0', 'utf-8');
$doc->recover = true;
$doc->strictErrorChecking = false;
//turning off some errors
libxml_use_internal_errors(true);
// it loads the content without adding enclosing html/body tags and also the doctype declaration
$doc->LoadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
echo $doc->saveHTML();
?>
Run Code Online (Sandbox Code Playgroud)
输出:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head></head>
<body>
<a role="tab"></a>
<input type="text">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
小智 4
如果属性名没办法DOMDocument接受的话,我们可以用特殊的字符串 before替换,after 替换回来@@loadHTML()saveHTML()
<?php
$content = <<<'EOT'
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head></head>
<body>
<a role="tab" @click="activeType=listingType"></a>
<input type="text" @autocomplete:change="handleAutocomplete">
</body>
</html>
EOT;
// creating new document
$doc = new DOMDocument('1.0', 'utf-8');
$doc->recover = true;
$doc->strictErrorChecking = false;
//turning off some errors
libxml_use_internal_errors(true);
$content = str_replace('@', 'at------', $content);
// it loads the content without adding enclosing html/body tags and also the doctype declaration
$doc->LoadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$html = $doc->saveHTML();
$html = str_replace('at------', '@', $html);
echo $html;
Run Code Online (Sandbox Code Playgroud)
输出:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head></head>
<body>
<a role="tab" @click="activeType=listingType"></a>
<input type="text" @autocomplete:change="handleAutocomplete">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
436 次 |
| 最近记录: |