【php-使用DOMDocument的Web刮板】教程文章相关的互联网学习教程文章

PHP DOMDocument:按类删除元素【代码】

我试图删除具有给定类的每个节点. 查找我使用的元素:$xpath = new DOMXPath($dom);foreach( $xpath->query('//div[contains(attribute::class, "foo")]') as $e ) {// Delete this node}但是,如何删除此foreach循环中的元素? 编辑:顺便说一句:我该如何首先检查DOM中是否存在带有“ foo”类的元素(在开始循环之前)? 更新: 这是我的HTML:<div class="main"><div class="delete_this" contenteditable="true">Target</div><div...

PHP:无法从DomDocument中删除节点【代码】

我无法从DomDocument中删除节点(获取异常): 我的代码:<?phpfunction 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...

php-将DOMDocument中的特定元素导出到字符串【代码】

我正在使用loadHTML()函数将一些任意HTML导入DOMDocument中,例如:$html = '<p><a href="test.php">Test</a></p>'; $doc = new DOMDocument; $doc->loadHTML($html);然后,我想使用DOMDocument方法更改一些属性/节点值,而我可以做到这一点. 完成这些更改后,我想导出HTML字符串(使用-> saveHTML()),而无需DOMDocument自动添加到HTML的< html>< body> …标签. 我知道为什么要添加这些内容(以确保文档有效),但是我该如何恢复已编辑的HT...

php – 删除父元素,使用saveHTML保留DOMDocument中的所有内部子元素【代码】

我正在使用XPath操作一个简短的HTML片段;当我使用$doc-> saveHTML()输出更改后的代码段时,会添加DOCTYPE,并且HTML / BODY标记会包装输出.我想删除它们,但只使用DOMDocument函数将所有子项保留在内部.例如:$doc = new DOMDocument(); $doc->loadHTML('<p><strong>Title...</strong></p> <a href="http://www....."><img src="http://" alt=""></a> <p>...to be one of those crowning achievements...</p>'); // manipulation goes...

php – 如何将XMLReader / DOMDocument与大型XML文件一起使用并防止500错误【代码】

我有一个大约12mb的XML文件,大约有16000个产品.我需要将它处理成一个数据库;然而,在大约6000行时,它会因500错误而死亡.我正在使用Kohana框架(版本3),以防万一与它有任何关系. 这是我在控制器中的代码:$xml = new XMLReader(); $xml->open("path/to/file.xml");$doc = new DOMDocument;// Skip ahead to the first <product> while ($xml->read() && $xml->name !== 'product');// Loop through <product>'s while ($xml->name == ...

php – DomDocument类无法访问domnode【代码】

我不解析这个网址:http://foldmunka.net$ch = curl_init("http://foldmunka.net");//curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here) $data = curl_exec($ch); $info = curl_getinfo($ch); curl_clos...

php – 用于解析HTML(而不是正则表达式)的DOMDocument【代码】

我正在尝试使用DOMDocument来解析HTML代码. 我只是在做一些简单的工作,我已经在scrap data using regex and simplehtmldom上喜欢gordon的答案,并根据他的工作编写了我的代码. 我发现PHP.net上的文档不是很好,因为信息有限,几乎没有示例,大多数细节都是基于解析XML.<?php $dom = new DOMDocument; libxml_use_internal_errors(true); $dom->loadHTMLFile('http://www.nu.nl/internet/1106541/taalunie-keurt-open-sourcewoordenlijs...

php – DOMDocument:如何通过换行符分隔内部HTML作为字符串?【代码】

<blockquote><p>2 1/2 cups sweet cherries, pitted<br>1 tablespoon cornstarch <br>1/4 cup fine-grain natural cane sugar</p> </blockquote>嗨,我想在’p’标签内得到文字.你看到有三条不同的线,我想在每行添加一些额外的文字后单独打印它们.这是我的代码块$tags = $dom->getElementsByTagName('blockquote');foreach($tags as $tag){$datas = $tag->getElementsByTagName('p');foreach($datas as $data){$line = $data->nodeV...

DOMDocument :: load – PHP – 获取属性值【代码】

我正在努力从ss获得3的值:ExpandedColumnCount =“3”in<Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="2" x:FullColumns="1"x:FullRows="1" ss:DefaultColumnWidth="65" ss:DefaultRowHeight="15"><Row><Cell><Data ss:Type="String">cat</Data></Cell><Cell><Data ss:Type="String">dog</Data></Cell><Cell><Data ss:Type="String">horse</Data></Cell></Row><Row><Cell><Data ss:Type="String">ve</Data></Cell><Cel...

php:每当我尝试编写UTF-8时都使用DomDocument,它会写入十六进制表示法【代码】

当我尝试使用DomDocument将UTF-8字符串写入XML文件时,它实际上写入字符串的十六进制表示法而不是字符串本身. 例如:&#x5D9;&#x5E8;&#x5D5;&#x5E9;&#x5DC;&#x5D9;&#x5DD;而不是:??????? 任何想法如何解决问题?解决方法:好的,你走了:$dom = new DOMDocument('1.0', 'utf-8'); $dom->appendChild($dom->createElement('root')); $dom->documentElement->appendChild(new DOMText('???????')); echo $dom->saveXml();将正常工作,因为...

加载时间:用PHP的DOMDocument或正则表达式解析HTML是否更快?【代码】

我正在将我的Flickr帐户中的图像拖到我的网站上,并且我使用了大约九行代码来创建一个可以拉动图像的preg_match_all函数. 我已多次阅读过,通过DOM解析HTML会更好. 就个人而言,我发现通过DOM解析HTML更加复杂.我编写了一个类似的函数来使用PHP的DOMDocument来提取图像,它大约有22行代码.创建需要一段时间,我不确定它的好处是什么. 每个代码的页面大约在同一时间加载,所以我不确定为什么我会使用DOMDocument. DOMDocument的工作速度是...

php – 带有XPath编码问题的DOMDocument. [?]很多测试【代码】

$msg = "<body><a>áéíóú????</a></body>"; $temp_dom = new DOMDocument(); $temp_dom->loadHTML($msg); $dom_xpath = new DOMXpath($temp_dom); $ele = $dom_xpath->query('//a')->item(0);echo "<pre>"; echo "Original: $msg\n"; echo $ele->nodeValue; echo "</pre>";输出:Original: áéíóú???? ?????-?3?oa?a¥a|a£当前文档编码为utf-8.我也试过ANSI,同样的问题也发生了. utf8_decode解决了这个问题echo utf...

php – 如何通过使用xpath和domdocument来切断内部html的一部分并将其存储为html字符串?【代码】

我想切断html的一些部分,我可以通过使用XPath和DomDocument来解决它,但问题是我需要结果作为HTML代码字符串.通常我会使用reg. EXPR.为此,但我不想做一个复杂的搜索模式,将标记的开头和结尾. 这是示例输入:some html code before <div>this <b>is</b> what I want</div> some html after和输出:<div>this <b>is</b> what I want</div>我试过这样的事情:subject = 'some html code before <div>this <b>is</b> what I want</div> ...

php – 用div替换带有DOMDocument的图像标记并返回更改的html【代码】

在下面的代码中,我试图用一些html替换$content中的每个图像.使用$dom-> saveHTML($image)我得到图像html,但str_replace不会替换它.我不知道为什么.$content = '<div class="content"><img src="some-image.png"></div>'; $dom = new DOMDocument; $dom->loadHTML($content); $images = $dom->getElementsByTagName('img'); foreach ($images as $image) {$i = $dom->saveHTML($image);$replacement = '<div class="test"><a href="...

UTF-8与PHP DOMDocument loadHTML?【代码】

考虑这个例子,test.php:<?php $mystr = "<p>Hello, με ???? ?a ?y je??</p>"; var_dump($mystr); $domdoc = new DOMDocument('1.0', 'utf-8'); //DOMDocument(); $domdoc->loadHTML($mystr); // already here corrupt UTF-8? var_dump($domdoc); ?>如果我用PHP 5.5.9(cli)运行它,我进入终端:$php test.php string(50) "<p>Hello, με ???? ?a ?y je??</p>" object(DOMDocument)#1 (34) {["doctype"]=>string(22) "(object ...

DOM - 相关标签