domdocument

DOM parser: remove certain attributes only

吃可爱长大的小学妹 提交于 2019-12-31 02:39:26
问题 How can I use DOM parser to remove all attributes in span tags but except these two attributes, <span style="text-decoration: underline;">cultura</span> accept <span style="text-decoration: line-through;">heart</span> accept reject this, <span style="font-family: " lang="EN-US">May</span> accept Is it possible? My working code from the other post I made, $content = ' <span style="text-decoration: underline;">cultura</span>l <span style="text-decoration: line-through;">heart</span> <span style

DomDocument removeChild in foreach reindexing the dom

社会主义新天地 提交于 2019-12-31 00:53:55
问题 I am trying to delete p tags with data-spotid attribute $dom = new DOMDocument(); @$dom->loadHTML($description); $pTag = $dom->getElementsByTagName('p'); foreach ($pTag as $value) { /** @var DOMElement $value */ $id = $value->getAttribute('data-spotid'); if ($id) { $value->parentNode->removeChild($value); } } but when i am removing child it is reindexing the dom. let suppose i have 8 items i deleted 1st it will reindex it and 2nd element will become 1st and it will not delete it will go to

XPATH - get single value returned instead of array php

◇◆丶佛笑我妖孽 提交于 2019-12-30 11:27:07
问题 I am using Xpath in PHP - I know that my query will return either 0 or 1 results. If 1 result is returned I do not want it as an array - which is what is returned right now. I simply want the value without having to access the [0] element of the result and cast to a string. Is this possible? 回答1: Using 'evaluate' instead of 'query' , you can do things like casting. DOMXPath::evaluate() Also, if you're just annoyed with doing stuff a lot of times, just write a function that does it ... that is

How to use XMLReader/DOMDocument with large XML file and prevent 500 error

≯℡__Kan透↙ 提交于 2019-12-30 05:29:04
问题 I have an XML file that is approximately 12mb which has about 16000 product's. I need to process it into a database; however, at about 6000 rows it dies with a 500 error. I'm using the Kohana framework (version 3) just in case that has anything to do with it. Here's my code that I have inside the controller: $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

DomDocument and html entities

本秂侑毒 提交于 2019-12-30 05:13:08
问题 I'm trying to parse some HTML that includes some HTML entities, like × $str = '<a href="http://example.com/"> A × B</a>'; $dom = new DomDocument; $dom -> substituteEntities = false; $dom ->loadHTML($str); $link = $dom ->getElementsByTagName('a') -> item(0); $fullname = $link -> nodeValue; $href = $link -> getAttribute('href'); echo " fullname: $fullname \n href: $href\n"; but DomDocument substitutes the text for for A × B. Is there some way to keep it from taking the & for an html entity and

PHP DOMDocument replace DOMElement child with HTML string

穿精又带淫゛_ 提交于 2019-12-30 03:17:08
问题 Using PHP I'm attempting to take an HTML string passed from a WYSIWYG editor and replace the children of an element inside of a preloaded HTML document with the new HTML. So far I'm loading the document identifying the element I want to change by ID but the process to convert an HTML to something that can be placed inside a DOMElement is eluding me. libxml_use_internal_errors(true); $doc = new DOMDocument(); $doc->loadHTML($html); $element = $doc->getElementById($item_id); if(isset($element))

How to import XML string in a php DOMDocument

天涯浪子 提交于 2019-12-30 02:21:32
问题 For exemple, i create a DOMDocument like that : <?php $implementation = new DOMImplementation(); $dtd = $implementation->createDocumentType ( 'html', // qualifiedName '-//W3C//DTD XHTML 1.0 Transitional//EN', // publicId 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-' .'transitional.dtd' // systemId ); $document = $implementation->createDocument('', '', $dtd); $elementHtml = $document->createElement('html'); $elementHead = $document->createElement('head'); $elementBody = $document->createElement(

How to import XML string in a php DOMDocument

爷,独闯天下 提交于 2019-12-30 02:21:27
问题 For exemple, i create a DOMDocument like that : <?php $implementation = new DOMImplementation(); $dtd = $implementation->createDocumentType ( 'html', // qualifiedName '-//W3C//DTD XHTML 1.0 Transitional//EN', // publicId 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-' .'transitional.dtd' // systemId ); $document = $implementation->createDocument('', '', $dtd); $elementHtml = $document->createElement('html'); $elementHead = $document->createElement('head'); $elementBody = $document->createElement(

document.getelementbyId will return null if element is not defined?

烂漫一生 提交于 2019-12-30 01:36:31
问题 In my code, I see this: if (document.getElementById('xx') !=null) { //do stuff } if xx element is not defined, will this evaluate to true or false? Should I write: if (document.getElementById('xx')) to be safe? 回答1: console.log(document.getElementById('xx') ) evaluates to null. document.getElementById('xx') !=null evaluates to false You should use document.getElementById('xx') !== null as it is a stronger equality check. 回答2: getElementById is defined by DOM Level 1 HTML to return null in the

php proDOM parsing error

纵然是瞬间 提交于 2019-12-29 07:20:51
问题 I am using the following code for parsing dom document but at the end I get the error "google.ac" is null or not an object line 402 char 1 What I guess, line 402 contains tag and a lot of ";", How can I fix this? <?php //$ch = curl_init("http://images.google.com/images?q=books&tbm=isch/"); // create a new cURL resource $ch = curl_init(); // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, "http://images.google.com/images?q=books&tbm=isch/"); curl_setopt($ch, CURLOPT_HEADER,