使用PHP修改HTML标记元素 [英] Modifying HTML tag elements using PHP
问题描述
我需要图片和图片之间的界限,删除了以下文字。我正在使用PHP。我正在使用DOMDocument来访问所有节点,并且我也能够获取节点路径。但我无法从节点路径修改该特定的HTML标记。有人能告诉我这是否可能吗?
这就是我现在所拥有的..非常感谢你们。
$ dom = new DOMDocument( );
$ dom-> loadHTMLFile($ pathname);
$ i = 0;
$ allNodes = $ dom-> getElementsByTagName('*');
$ tagNamesArray = array();
foreach($ allNodes as $ node){
$ tagNodePath = $ node-> getNodePath();
$ tagName = end(explode('/',$ node-> getNodePath()));
$ tagNamesArray [$ i] [1] = $ tagName;
$ tagNamesArray [$ i] [2] = $ tagNodePath;
$ i ++;
}
checkForLines($ tagNamesArray,$ dom);
函数checkForLines($ tagsArray,$ dom){
$ xPath = new DOMXpath($ dom);
for($ i = 0; $ i<(count($ tagsArray)-1); $ i ++){
if($ tagsArray [$ i] [1] =='img'& & $ tagsArray [$ i + 1] [1] =='br'){
echo $ tagsArray [$ i + 1] [2]。'< br>';
$ lineTag = $ xPath->查询($ tagsArray [$ i + 1] [2]);
$ domElement = $ dom-> removeChild($ lineTag);
>
...考虑任何图片, < h $ gt;< / code>等等...)之间的图像和文字有< br>
标签, p>
如果您想这样做:
$ dom = new DOMDocument;
$ dom-> loadHTML(< img src ='foo.png'/>< br />< p> Hello World< / p>
$ img = $ dom-> getElementsByTagName(img);
foreach($ img as $ current){
$ sibling = $ current-> nextSibling;
if($ sibling-> nodeName ===br)
$ current-> parentNode-> removeChild($ sibling);
}
echo $ dom-> saveHTML();
其结果如下:
< img src =foo.png>< p> Hello World< / p>
I have been trying to modify HTML tag elements, I have a huge list of HTML files which needs to be modified.
I need the lines between the images and the following text removed. I am using PHP. I am using a DOMDocument to access all the nodes and I am also able to get the nodepath. But I am unable to get to modify that particular HTML tag from the nodepath. Can someone tell me if this is possible?
This is what I have as of now.. Thanks a lot ppl..
$dom = new DOMDocument();
$dom->loadHTMLFile($pathname);
$i=0;
$allNodes = $dom->getElementsByTagName('*');
$tagNamesArray = array();
foreach($allNodes as $node) {
$tagNodePath = $node->getNodePath();
$tagName = end(explode('/',$node->getNodePath()));
$tagNamesArray[$i][1] = $tagName;
$tagNamesArray[$i][2] = $tagNodePath;
$i++;
}
checkForLines($tagNamesArray, $dom);
function checkForLines($tagsArray, $dom) {
$xPath = new DOMXpath($dom);
for($i=0 ; $i<(count($tagsArray)-1) ; $i++) {
if($tagsArray[$i][1] == 'img' && $tagsArray[$i+1][1] == 'br') {
echo $tagsArray[$i+1][2].'<br>';
$lineTag = $xPath->query($tagsArray[$i+1][2]);
$domElement = $dom->removeChild($lineTag);
}
}
}
解决方案
...consider any image and after it some following text(be it <p>
or <h1>
etc...) between the image and text there are <br>
tags which I want removed...
If this is all you want to do:
$dom = new DOMDocument;
$dom->loadHTML( "<img src='foo.png' /><br/><p>Hello World</p>" );
$img = $dom->getElementsByTagName("img");
foreach ( $img as $current ) {
$sibling = $current->nextSibling;
if ( $sibling->nodeName === "br" )
$current->parentNode->removeChild( $sibling );
}
echo $dom->saveHTML();
Which results in the following output:
<img src="foo.png"><p>Hello World</p>
这篇关于使用PHP修改HTML标记元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!