Forum Moderators: coopster

Message Too Old, No Replies

Pulling a line out of an HTML document stored in a PHP variable

         

JesseBarksdale

6:10 am on Aug 19, 2009 (gmt 0)

10+ Year Member



Hello,

I am trying to figure out how to pull a specific line out of an HTML file I have stored in a PHP variable. I have used the following to store the page in a PHP variable:

$url = "http://www.imdb.com";

$str = file_get_contents($url);

And now want to be able to pull out just one line from the HTML doc to be able to manipulate it. For instance, if somewhere in the document there was

<head> data="120312039102390123" </head>

I want to run through and pull out 120312039102390123 into my own variable. I am trying to display and manipulate data from another webpage on my own site. It seems like there would be a simple PHP function to do this, but I keep coming across crazy XHTML parsing stuff that I don't understand.

Any help you might be able to offer would be greatly appreciated!

- Jesse

eelixduppy

4:07 am on Aug 20, 2009 (gmt 0)



Sounds like the job for regular expressions [us2.php.net].

Perhaps something like this:


$pattern = "/<head>.*data=\"([0-9]+)\".*<\/head>/i":
preg_match($pattern, $str, $matches);
echo '<pre>'; print_r($matches); echo '</pre>';

This was untested but you should get the idea. Read up on the link I gave you for additional information regarding the pattern syntax.