我有一个名为变量的变量$articleText,它包含html代码.有script和style内码<script>和<style>HTML元素.我想扫描$articleText并删除这些代码.如果我还可以删除实际的HTML元素<script>,</script>,<style>和</style>,我会做到这一点.
我想我需要使用正则表达式,但我不熟练.
有人可以帮忙吗?
我希望我能提供一些代码,但就像我说我不熟练的正则表达式,所以我没有任何东西可以显示.
我无法从DomDocument中删除节点(获取异常):
我的代码:
<?php
function filterElements($htmlString) {
$doc = new DOMDocument();
$doc->loadHTML($htmlString);
$nodes = $doc->getElementsByTagName('a');
for ($i = 0; $i < $nodes->length; $i++) {
$node=$nodes->item($i)
if ($value->nodeValue == 'my_link') {
$doc->removeChild($node);
}
}
}
$htmlString = '<div>begin..</div>this tool<a name="my_link">Beo</a> great!<div>.end</div>';
filterKeyLinksElements($htmlString);
?>
Run Code Online (Sandbox Code Playgroud)
谢谢你,约瑟夫
使用这段XML:
<my_xml>
<entities>
<image url="lalala.com/img.jpg" id="img1" />
<image url="trololo.com/img.jpg" id="img2" />
</entities>
</my_xml>
Run Code Online (Sandbox Code Playgroud)
我必须摆脱图像标签内的所有属性.所以,我做到了这一点:
<?php
$article = <<<XML
<my_xml>
<entities>
<image url="lalala.com/img.jpg" id="img1" />
<image url="trololo.com/img.jpg" id="img2" />
</entities>
</my_xml>
XML;
$doc = new DOMDocument();
$doc->loadXML($article);
$dom_article = $doc->documentElement;
$entities = $dom_article->getElementsByTagName("entities");
foreach($entities->item(0)->childNodes as $child){ // get the image tags
foreach($child->attributes as $att){ // get the attributes
$child->removeAttributeNode($att); //remove the attribute
}
}
?>
Run Code Online (Sandbox Code Playgroud)
不知何故,当我尝试删除foreach块中的from属性时,它看起来像内部指针丢失,并且它不会删除这两个属性.
还有另一种方法吗?
提前致谢.