views:

50

answers:

2

I need to parse the following sample html using xpath query..

<td id="msgcontents">
 <div class="user-data">Just seeing if I can post a link... please ignore post
  <a href="http://finance.yahoo.com"&gt;http://finance.yahoo.com&lt;/a&gt;
 </div>
</td>

<td id="msgcontents">
 <div class="user-data">some text2...
  <a href="http://abc.com"&gt;http://abc.com&lt;/a&gt;
 </div>
</td>

<td id="msgcontents">
 <div class="user-data">some text3...      
 </div>
</td>

The above html may repeat n no of times in a page.

Also sometimes the ..... portion may be absent as shown in the above html blocks.

What I need is the xpath syntax so that I can get the parsed strings as

 array1[0]= "Just seeing if I can post a link... please ignore post ttp://finance.yahoo.com" 
 array[1]="some text2 htp://abc.com"
 array[2]="sometext3" 
A: 

Maybe something like the following:

   $remote = file_get_contents('http://www.sitename.com');
    $dom = new DOMDocument();
    //Error suppression unfortunately, as an invalid xhtml document throws up warnings.
    $file = @$dom->loadHTML($remote);

    $xpath = new DOMXpath($dom);

    //Get all data with the user-data class.
    $userdata = $xpath->query('//*[contains(@class, \'user-data\')]');

    //get links
    $links = $xpath->query('//a/@href');

So to access one of these variables, you need to use nodeValue:

$ret = array();
foreach($userdata as $data) {
  $ret[] = $data->nodeValue;
}

Edit: I thought I'd mention that this will get all the links on a given page, I assume this is what you wanted?

Russell Dias
A: 

Use:

concat(/td/div/text[1], ' ', /td/div/a)

You can use instead of the ' ' above, whatever delimiter you'd like to appear between the two strings.

Dimitre Novatchev
Thanks a lot.. I tried your solution but did not get my result. I have edited the question. Please review it.
djk