domdocument

PHP DOMDocument saveHTML not encoding cyrillic correctly

浪子不回头ぞ 提交于 2019-12-23 17:50:18
问题 I use DOMDocument to manipulate html and php 7. The problem is that text shows good on page (cyrillic), but when I go to "See HTML page source", it is not good . It shows like this: Здесь осн What might be wrong? <meta> charset is utf-8. My code: $dom = new DOMDocument(); if (@$dom->loadHTML(mb_convert_encoding("<div>$body</div>", 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD)) { // https://stackoverflow.com/questions/29493678/loadhtml-libxml-html-noimplied-on-an

How do I get the src attribute of img tags?

天涯浪子 提交于 2019-12-23 17:09:03
问题 I load the DOM by an external url as such: $dom = new DOMDocument; $dom->loadHTMLFile( "external_url.html" ); $arrayOfSources = array(); foreach( $dom->getElementsByTagName( "img" ) as $image ) $arrayOfSources[] = $image->item(0)->getAttribute("src"); This way I want to store all the src attributes of the img tags in an array, but I keep getting the error Fatal error: Call to undefined method DOMDocument::item() What am I missing here? How do I extract all the src attributes from the img tags

Xpath works on “MSXML2.DOMDocument” but not on “MSXML2.DOMDocument60” [duplicate]

佐手、 提交于 2019-12-23 12:27:37
问题 This question already has an answer here : Closed 8 years ago . Possible Duplicate: String greater, less, and equal comparison in XmlDocument Hi, In VBA I have the folowing expression: SourceXml.selectNodes("//Races/Race[/FirstRun[@ActStart>'2011-03-01' or @ActEnd<'2011-03-15']]") If I define the SourceXml as MSXML2.DOMDocument it retrieves a list with the desired nodes. If I define the SourceXml as MSXML2.DOMDocument60 it retrieves a list with 0 elements inside. Whath is wrong with the Xpath

PHP DOMDocument move nodes from a document to another

岁酱吖の 提交于 2019-12-23 11:31:08
问题 OK, I'm trying to achieve this for hours now and can't seem to find a solution so here I am! I have 2 DOMDocument and I want to move the nodes of a document to the other one. I know the structure of both documents and they are of the same type (so I should have no problem to merge them). Anyone can help me? If you need more info let me know. Thanks! 回答1: To copy (or) move nodes to another DOMDocument you'll have to import the nodes into the new DOMDocument with importNode(). Example taken

Why DomDocument getElementsByTagName give back an half NodeList?

我与影子孤独终老i 提交于 2019-12-23 09:34:23
问题 I generate some non-standard-tag HTML with DomDocument and the result is this: /* Input HTML <div id="toobar_top"> <widget id="flag_holder"></widget> <widget id="horizontal_menu"></widget> </div> <div id="header"> <widget name="header"></widget> </div> */ What I want to do is to "translate" each widget in something useful... they are simple placeholders with params. The function extract from the class is: private function widgeter($doc) { //Give it an DomDocument HTML containing <widget>

php DOMDocument adds <html> headers with DOCTYPE declaration

假如想象 提交于 2019-12-23 07:38:38
问题 I'm adding a #b hash to each link via the DOMDocument class. $dom = new DOMDocument(); $dom->loadHTML($output); $a_tags = $dom->getElementsByTagName('a'); foreach($a_tags as $a) { $value = $a->getAttribute('href'); $a->setAttribute('href', $value . '#b'); } return $dom->saveHTML(); That works fine, however the returned output includes a DOCTYPE declaration and a <head> and <body> tag. Any idea why that happens or how I can prevent that? 回答1: That's what DOMDocument::saveHTML() generally does,

Trouble fetching some title from a webpage

喜你入骨 提交于 2019-12-23 07:25:43
问题 I've written a script in php to scrape a title visible as hair fall shamboo from a webpage. When I execute my below script, I get the following error: Notice: Trying to get property 'nodeValue' of non-object in C:\xampp\htdocs\runcode\testfile.php on line 16. Link to that site Script I've tried with: <?php function get_content($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl

Trouble fetching some title from a webpage

懵懂的女人 提交于 2019-12-23 07:25:01
问题 I've written a script in php to scrape a title visible as hair fall shamboo from a webpage. When I execute my below script, I get the following error: Notice: Trying to get property 'nodeValue' of non-object in C:\xampp\htdocs\runcode\testfile.php on line 16. Link to that site Script I've tried with: <?php function get_content($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl

PHP getting and setting attributes on HTML Elements [duplicate]

烂漫一生 提交于 2019-12-23 07:08:35
问题 This question already has answers here : PHP Getting and Setting tag attributes (2 answers) Closed 6 years ago . I'm looking for a solution for manipulating html elements via php. I was reading http://www.php.net/manual/en/book.dom.php but I didn't get to far. I'm taking an "iframe" element ( video embed code ) and trying to modify it before echoing it. I would like to add some parameters to the "src" attribute. Based on the answer from https://stackoverflow.com/a/2386291 I'am able to iterate

How to “refresh” DOMDocument instances of LibXML2?

风格不统一 提交于 2019-12-23 05:29:20
问题 Using PHP to illustrate: there are a BUG in the normalizeDocument() method, or a lack of a "refresh" method, because DOM consistence is lost after changes (even only attribute changes)... So, any algorithm "with DOM changes" that you implement with LIBXML2 somethimes works and sometimes not, is unpredictable!! (?) The "refresh" by $doc->LoadXML($doc->saveXML()); is a workaround and lost performance in a flow of work with DOM... A sub-question: all moment I need to refresh DOM? $XML = ' <html>