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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.