jjzjj

DomDocument

全部标签

php - 如何获取 img 标签的 src 属性?

我通过外部url加载DOM:$dom=newDOMDocument;$dom->loadHTMLFile("external_url.html");$arrayOfSources=array();foreach($dom->getElementsByTagName("img")as$image)$arrayOfSources[]=$image->item(0)->getAttribute("src");这样我想将img标签的所有src属性存储在一个数组中,但我不断收到错误Fatalerror:CalltoundefinedmethodDOMDocument::item()我在这里错过了

php - 加载时间 : is it quicker to parse HTML with PHP's DOMDocument or with Regular Expressions?

我正在将我的Flickr帐户中的图像提取到我的网站,我使用了大约九行代码来创建一个可以提取图像的preg_match_all函数。我读过好几遍,通过DOM解析HTML会更好。就我个人而言,我发现通过DOM解析HTML更加复杂。我用PHP的DOMDocument编写了一个类似的函数来拉取图像,它大约有22行代码。创建花了一些时间,我不确定有什么好处。每个代码的页面加载时间大致相同,所以我不确定为什么要使用DOMDocument。DOMDocument是否比preg_match_all工作得更快?如果您有兴趣,我会向您展示我的代码(您可以看到DOMDocument代码有多长)://here

php - 在 xml 文件的特定点插入 xml

我想在我的xml文件中插入以下行:紧接着:在我的xml文件中。目前我使用这种(糟糕的)方法:$G['xml']=str_replace('','',$G['xml']);在php中使用DomDocument的正确方法是什么?谢谢 最佳答案 您要插入的行称为processinginstruction.您可以像这样使用DOM添加它:$dom=newDOMDocument();$dom->loadXml('');$dom->insertBefore($dom->createProcessingInstruction('xml-styles

php - 具有 XPath 编码问题的 DOMDocument。 [?] 很多测试

$msg="áéíóú☻♥♦♣";$temp_dom=newDOMDocument();$temp_dom->loadHTML($msg);$dom_xpath=newDOMXpath($temp_dom);$ele=$dom_xpath->query('//a')->item(0);echo"";echo"Original:$msg\n";echo$ele->nodeValue;echo"";输出:Original:áéíóú☻♥♦♣áéíóúâ»â¥â¦â£当前文档编码为utf-8。我也试过ANSI并且发生了同样的问题。utf8_decode解决问题echoutf8_de

php - 使用 PHP 解析 html 并循环遍历表格行和列?

我正在尝试从loadHTML解析HTML但我遇到了麻烦,我设法遍历了所有s在文档中,但我不知道如何遍历每行s。这是我目前所做的:$DOM->loadHTML($url);$rows=$DOM->getElementsByTagName('tr');for($i=0;$ilength;$i++){//loopthroughrows//loopthroughcolumns...}如何循环遍历每一行中的列? 最佳答案 DOMElement还支持getElementsByTagName:$DOM=newDOMDocument();$DOM-

php - 按名称获取元素 - HTML DOM 短语

我正在尝试使用HTMLDOM短语来表达html源代码,我需要在输入标签中获取一个值,我试过这个:foreach($doc->getElementsByTagName('input')as$link){$links[]=array('value'=>$link->getAttribute('value'),'text'=>$link->nodeValue,);}这对我有用,但我的网页有多个输入标签,但我想获取指定输入标签的值。假设是,我尝试使用getElementsByTagName,但它给了我一个错误。 最佳答案 您也可以使用您现在

PHP 使用 DOMXPath 剥离标签和删除节点

我正在尝试使用DOMDocument,但遇到了一些问题。我有一个这样的字符串:SomeContenttokeepThiscontentshouldremain,butspanarounditshouldbestrippedKeepthiscontenttooThiswholenodeshouldbedeleted我想做的是,如果span有一个类似ice-del的类保留内部内容但删除span标签。如果它有ice-ins,删除整个节点。如果只是一个空span也将其删除。这是我的代码://thisgettheabovementionedstring$getVal=$array['body']

php - CData section not finished 问题

当我对下面的XML使用DOMDocument::loadXML()时,出现错误:Warning:DOMDocument::loadXML()[domdocument.loadxml]:CDatasectionnotfinishedhttp://www.site.org/displayimage.php?album=seinEntity,Warning:DOMDocument::loadXML()[domdocument.loadxml]:Prematureendofdataintagimageline7inEntityWarning:DOMDocument::loadXML()[dom

php - 从 DOM 对象中删除跨度但不删除内容并将其保存到变量中

假设我有以下字符串:Thisisanexample我想删除跨度而不是内容。$content='Thisisanexample';$dom=newDOMDocument();$dom->loadXML($content);$nodes=$dom->getElementsByTagName('span');foreach($nodesas$node){//removespanbutnotcontent}$dom->save($var);//$dom->save()savestofilebutIwanttosaveto$var所以$var包含:Thisisanexample.所以基本上我有两

php - 如何提取 anchor 标记内的所有 img 标记?

我想使用PHPDOM对象提取anchor标记内的所有img标记。我正在尝试使用下面的代码,但由于img标签的内部,它获取了所有anchor标签并使其文本为空。functionget_links($url){//CreateanewDOMDocumenttoholdourwebpagestructure$xml=newDOMDocument();//Loadtheurl'scontentsintotheDOM@$xml->loadHTMLFile($url);//Emptyarraytoholdalllinkstoreturn$links=array();//Loopthrougheac