domdocument

PHP DOMDocument: inserBefore, how to make it work ?

Hi, I would like to place a new node element, before a given element. I'm using insertBefore for that, without success! Here's the code, <DIV id="maindiv"> <!-- I would like to place the new element here --> <DIV id="child1"> <IMG /> <SPAN /> </DIV> <DIV id="child2"> <IMG /> <SPAN /> </DIV> //$div is a ne...

How to keep DOMDocument from saving < as &lt

I'm using simpleXML to add in a child node within one of my XML documents... when I do a print_r on my simpleXML object, the < is still being displayed as a < in the view source. However, after I save this object back to XML using DOMDocument, the < is converted to &lt; and the > is converted to &gt; Any ideas on how to change this beh...

DOMDocument need to search for an element that has attribute class="something"

In PHP I'm using DOMDocument and I need to search for an element that has attribute class="something" I'm new with DOMDocument (been using REGEX all my life so help me :P) Ok, my question is, I've found a DOMElement using getElementById, now i want to look inside the children of this element and retrieve a node that has a particular cl...

Porting PHP5 to legacy PHP4, DOMDocument quibbles

I'm trying to make some of my php5 code work on a legacy server, which unfortunately cannot be upgraded (client's machine). if (!isset($docRoot)) { $docRoot = $_SERVER['DOCUMENT_ROOT']; } // generic storage class for the words/phrases $t = new stdClass(); $t->lang = $curPage->lang; // load xml translations, could split this int...

DOMDocument encoding problems / characters transformed

I am using DOMDocument to manipulate / modify HTML before it gets output to the page. This is only a html fragment, not a complete page. My initial problem was that all french character got messed up, which I was able to correct after some trial-and-error. Now, it seems only one problem remains : ' character gets transformed into ? . Th...

php: using DomDocument whenever I try to write UTF-8 it writes the hexadecimal notation of it.

Hello. When I try to write UTF-8 Strings into an XML file using DomDocument it actually writes the hexadecimal notation of the string instead of the string itself. for example: &#x5D9;&#x5E8;&#x5D5;&#x5E9;&#x5DC;&#x5D9;&#x5DD; instead of: ירושלים any ideas how to resolve the issue? ...

php domdocument weird formating

Hi Guys, I am experiencing weird behaviour when trying to format xml output while modifying domdocument structure. I have created simple Item class based on DomDocument: class Item extends DOMDocument { private $root; function __construct($version = null, $encoding = null) { parent::__construct($version, $encoding); $this->f...

Is a <head> element always available in the DOM, even if absent in the HTML markup?

Every browser I've observed creates a <head> element that's accessible in the DOM even if there are no explicit <head></head> tags in the document's markup. However, Google Analytics uses the following code for dynamic script insertion: (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async...

PHP: Can't remove node from DomDocument

Hi, I Can't remove node from DomDocument(get Exception): My Code: <?php function filterElements($htmlString) { $doc = new DOMDocument(); $doc->loadHTML($htmlString); $nodes = $doc->getElementsByTagName('a'); for ($i = 0; $i < $nodes->length; $i++) { $node=$nodes->item($i) if ($va...

Deferrable document.write

Hello, alot of web advertising providers use the old document.write method, in my case AdTech. Is it possible to overwrite the document.write method to defer after the onload event? Kind regards ...

get div around searched keyword (file_get_contents('url')

So im creating a webcrawler and everything works, only got 1 problem. With file_get_contents($page_data["url"]); I get the content of a webpage. This webpage is scanned when one of my keywords excists on the webpage. $find = $keywords; $str = file_get_contents($page_data["url"]); if(strpos($str, $find) == true) When i want to insert...

DOMDocument XPath

Can somebody show me some examples to import a html-page and use the XPath to find the keywords including the rest of the text from the div, p, title etc. Thank you! EDIT: In this case i use my webcrawler for example, i have a form to get the website to be crawled and the keywords wich has to be find in pages of the website. http://c...

PHP: Recursively Process then remove DOMElements from DOMDocument

I'm using PHP's DOMDocument and related classes to work with XML. The XML contains processing instructions that must be processed in a specific order, from top to bottom, deepest to shallowest. I'm using a recursive function that takes the entire DOMDocument, then calls itself foreach child node, all the way down. I need to delete some...

Is there a way to keep entities intact while parsing html with DomDocument?

I have this function to ensure every img tag has absolute URL: function absoluteSrc($html, $encoding = 'utf-8') { $dom = new DOMDocument(); // Workaround to use proper encoding $prehtml = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset={$encoding}\"></head><body>"; $posthtml = "</body></html>...

How do I serialize DOM into XML using PHP?

I am attempting to send html data in a question form from my php web application to mechanical turk so a user can see the entire html document from an email to work with. I have had difficulty thus far. In the thread linked below, I attempted to parse the html data using html5-lib.php, but I think I'm still missing a step in order to c...

PHP: Creating a DOMDocument works fine on MAMP Testing Enviroment, fails on external Server

Hi everyone, i want to load the content of different html-files (all on the same server/in the same directory) for further processing. For this i want to use the loadHTMLfile method, but it seems i'm not able to create a DOMDocument object on the external Server. Testcode: <?php if(!class_exists('DOMDocument')){ echo 'No DOM! '; } if(...

Parse HTML with PHP's HTML DOMDocument

Hi guys This is my example script: $html = <<<HTML <div class="main"> <div class="text"> Capture this text 1 </div> <div class="date"> May 2010 </div> </div> <div class="main"> <div class="text"> Capture this text 2 </div> <div class="date"> June 2010 </div> </div> HTML; $dom = new DOMDo...

PHP Getting parent's ID => child's value as array from XML

I have an XML file structured like this <serieslist> <series sid="123"> <title type="main">Series 123 Main Title</title> <title type="official">Series 123 Official Title</title> <title type="short">S 123</title> </series> <series sid="456"> <title type="main">Series 456 Main Title</title> ...

Find h3 and h4 tags beneath it

This is my HTML: <h3>test 1</h3> <p>blah</p> <h4>subheading 1</h4> <p>blah</p> <h4>subheading 2</h4> <h3>test 2</h3> <h4>subheading 3</h4> <p>blah</p> <h3>test 3</h3> I am trying to build an array of the h3 tags, with the h4 tags nested within them. An example of the array would look like: Array ( [test1] => Array ( ...

load DOMDocument with HTML Special Characters (php)

Hi, i have a problem to load a xml-file with php. I use DOMDocument, because i need the function getElementsByTagName. I use this code. $dom = new DomDocument('1.0', 'UTF-8'); $dom->resolveExternals = false; $dom->load($_FILES["file"]["tmp_name"]); <?xml version="1.0" encoding="UTF-8"?> <Data> <value>1796563</value> <value>Verli...