views:

31

answers:

3

Hiya.

Lets say i have the following web page:

<html>
<body>
 <div class="transform">
    <span>1</span>
 </div>
 <div class="transform">
    <span>2</span>
 </div>
 <div class="transform">
    <span>3</span>
 </div>
</body>
</html>

I would like to find all div elements that contain the class transform and to fetch the text in each div element ?

I know I can do that easily with regular expressions, but i would like to know how can I do that without regular expressions, but parsing the xml and finding the required nodes i need.

update

i know that in this example i can just iterate through all the divs. but this is an example just to illustrate what i need.

in this example i need to query for divs that contain the attribute class=transform

thanks!

+1  A: 

Could use SimpleXML - see the example below:

$string = "<?xml version='1.0'?> 
<html>
<body>
 <div class='transform'>
    <span>1</span>
 </div>
 <div>
    <span>2</span>
 </div>
 <div class='transform'>
    <span>3</span>
 </div>
</body>
</html>";

$xml = simplexml_load_string($string);
$result = $xml->xpath("//div[@class = 'transform']");

foreach($result as $node) {
  echo "span " . $node->span . "<br />";
}

Updated it with xpath...

xil3
i want to query for divs that contains the attribute `class=transform`
ufk
updated the main post with more info
ufk
Just updated the answer - that should do what you need.
xil3
A: 

ok what i wanted can be easily achieved using php xpath:

example:

http://ditio.net/2008/12/01/php-xpath-tutorial-advanced-xml-part-1/
ufk
+1  A: 

You can use xpath to address the items. For that particular query, you'd use:

div[contains(concat(" ",@class," "), concat(" ","transform"," "))]

Full PHP example:

<?php
  $document = new DomDocument();
  $document->loadHtml($html);
  $xpath = new DomXPath($document);
  foreach ($xpath->query('div[contains(concat(" ",@class," "), concat(" ","transform"," "))]') as $div) {
    var_dump($div);
  }

If you know CSS, here's a handy CSS-selector to XPath-expression mapping: http://plasmasturm.org/log/444/ -- You can find the above example listed there, as well as other common queries.

If you use it a lot, you might find my csslib library handy. It offers a wrapper csslib_DomCssQuery, which is similar to DomXPath, but using CSS-selectors instead.

troelskn