View Full Version : PHP Regular Expressions to extract an integer

08 Jul 2010, 01:53 PM

I'm new here but am looking for some advice on how to extract a specific string of text from a website.

Basically, i want to echo back the friend count on a myspace profile page ( http://www.myspace.com/rachelfurner )

I would like to extract the integer '9762' (and only the integer) from the following string: Rachel Furner has <span class="redbtext" property="myspace:friendCount">9724</span> friends.

I gather i can do this by loading the page via file_get_contents and then using RegEx to extract the integer but am unsure as to the preg_match value for extracting this specific information.

Any advise on the best way to do this whether via Regular Expressions or not, would be much appreciated.


Dave Grant

09 Jul 2010, 01:27 PM

preg_match('/\<span class\=\"redbtext\" property\=\"myspace\:friendCount\"\>([0-9]+)\<\/span\>/', $string, $matches);

// This will be the count
echo $matches[1];


I haven't tested this but it SHOULD work.