3

Possible Duplicate:
How to parse and process HTML with PHP?

How would I go about getting a certain string from a webpage that has been scraped? I am using SimpleBrowser in PHP to download a webpage into a variable.

The resultant webpage at a certain part has the following:

    <tr>
        <td class="label" width="350">POD Receiver Name:&nbsp;</td>
        <td class="field" align="left">
            <b>KRISTY</b>&nbsp;
        </td>
    </tr>

I want to get the value KRISTY into a variable, but not really sure how. I have no real experience with regex so I wouldnt know where to start.

Any help appreciated!

Community
  • 1
  • 1
Lock
  • 5,422
  • 14
  • 66
  • 113

3 Answers3

1

To pull one specific part out from a known location, I'd use xpath. Try a tutorial such as http://ditio.net/2008/12/01/php-xpath-tutorial-advanced-xml-part-1/

walrii
  • 3,472
  • 2
  • 28
  • 47
0

I am not sure why you are storing a page in a variable. But if you have a page stored as a string in a variable you can use Regular expression to extract string out of it. For this particular example you can use something like this.

$v = '<tr>
       <td class="label" width="350">POD Receiver Name:&nbsp;</td>
       <td class="field" align="left">
        <b>KRISTY</b>&nbsp;
      </td>
    </tr>';

preg_match('/\<b\>(.*?)\<\/b\>/', $v, $matches);
$result = $matches[1];

This particular regular expression gets everything between the bold tags.

robonerd
  • 198
  • 8
0

If the structure can be depended on, give SimpleXML a shot:

$xml = simplexml_load_string(html_entity_decode($v));
$name = strval($xml->td[1]->b);//KRISTY

http://php.net/manual/en/function.simplexml-load-string.php

http://www.php.net/manual/en/class.simplexmlelement.php

Shad
  • 15,134
  • 2
  • 22
  • 34