Jump to content

Preg matching multiple things on 1 webpage?


sphinx

Recommended Posts

Hello,

 

I basically host a radio and want to preg_match the important information onto another website. Up until now, I file_get_contents like 4 times and preg_matched but required the site to be grabbed like 5 times before it even loaded. The information I want is something like this:

 

20120829.202849.png

 

I tested something like this:

 

<?php
$page_contents = file_get_contents("http://sitewherethepageaboveislocated.co.uk");
$matches = array();
preg_match('/([0-9]+) of 50/', $page_contents, $matches); //current listeners
preg_match('~Kadio :.*?)</b>~i',$page_contents, $matches2); //what DJ is playing
echo $matches[0];
echo $matches2[0];
?>

 

But it seems to be overly complicated when trying to preg_match loads of factors, is this the only way to do it?

 

thanks

 

Depending on the page there could be easier ways of doing it. If it's well-formed HTML then you can read it through DOMDocument. For instance, with

Server Status:
Server is currently up and private

you could do a getElementById("server-status") to get that TD node. More complicated would be a getElementsByTagName, and worse would be manually traversing the hierarchy (ie, through childNodes and such). But IMO all three are preferable to regular expressions.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.