Re: DOM Functions (I think)
Anyone have any ideas? Maybe not with PHP, but with javascript or something? I'm totally have no idea how to do this, Google didnt really comeup with anything. I just know how to do it in VB, using GetElementByID.
Re: DOM Functions (I think)
What version of PHP are you using?
Re: DOM Functions (I think)
Uhh... 5 I think. I found this but it's not that helpful...
HTML Code:
http://ca3.php.net/manual/en/function.dom-domdocument-loadhtmlfile.php
http://ca3.php.net/manual/en/function.dom-domdocument-getelementbyid.php
Re: DOM Functions (I think)
If you are using PHP 5 it is quite simple. The DOM extension which is included by default enables you to load HTML into a DOMDocument object and treat it as you would any other XML document.
PHP Code:
$doc = new DOMDocument('1.0'); // 1.0 is the XML version
$doc->loadHTMLFile('http://www.example.com/test.html');
$tables = $doc-getElementsByTagName('table');
If you only have PHP 4, you'll need to use the DOM XML extension. As well as not complying with the official W3C DOM specification, it is also not included by default. My advice if your host only has PHP 4, is to find another. :)
Re: DOM Functions (I think)
Ok, cool, so do I get the contents of a table cell? The first cell contains a picture, the other one is just some text.
Re: DOM Functions (I think)
The easiest way to get a refernce to the image element you need is to give it an ID and then use the getElementById function.
Re: DOM Functions (I think)
Ok... so lets say that the id of the cell is "usrpic", how do I save the image location to a variable?
EDIT: Actually the image and the cell don't have an ID, the <a href> surrounding the image does. ALso, I need to get something from between a <span> tag which also has no id, but has a unique class stlye name...
Re: DOM Functions (I think)
Using DOM you would first get the ID of the anchor, then get the img element:
PHP Code:
$a = $doc->getElementById('idofa');
$imgs = $a->getElementsByTagName('img');
$src = $imgs->item(0)->getAttribute('src');
You could be sneaky however and use a single xPath expression to pull out the info you need:
PHP Code:
$doc = new DOMDocument('1.0');
$doc->loadHTMLFile('dom.html');
$xpath = new DOMXPath($doc);
$src = $xpath->evaluate("string(//*[@id='blah']/img[1]/@src)");
You can see it work on dom.html here:
http://php5.codedv.com/examples/dom/dom.php (source)
Re: DOM Functions (I think)
Great thanks, I'll try that.
On a somewhat related topic, who can I load the html for a website into a variable, insert my own (javascript) and then echo it back? For example, take mywebsite "http://www.bluecable.ca/index.php" and add the adsense javascript to the top and then display the page?