Jump to content

Extracting data from text/string


computer guy

Recommended Posts

Hi everyone,

 

 

Is there a way that I can extract the following

 

etaRadio Ratings Dump – Current at 26/03/09<--The date

DJ Joelstar                          14 12 14 15 18 21 16 20.17<--These eight numbers (seperate) and the DJ name

 

 

From a string like this

etaRadio Ratings Dump – Current at 26/03/09
DJ Joelstar                           14 12 14 15 18 21 16 20.17
DJ Ant                                   0 0 0 0 0 0 0 0.00
DJ Jaxon                               18 16 14 19 21 14 17 21.89
DJ Chuckmeister85          19 21 24 32 24 18 23 29.61
DJ Pat4life                           21 20 24 28 21 18 22 28.33
DJ JessieB                            0 0 0 0 0 0 0 0.00

 

Thank you  :)

Link to comment
https://forums.phpfreaks.com/topic/151191-extracting-data-from-textstring/
Share on other sites

try

<?php
$test = 'etaRadio Ratings Dump – Current at 26/03/09
DJ Joelstar                           14 12 14 15 18 21 16 20.17
DJ Ant                                   0 0 0 0 0 0 0 0.00
DJ Jaxon                               18 16 14 19 21 14 17 21.89
DJ Chuckmeister85          19 21 24 32 24 18 23 29.61
DJ Pat4life                           21 20 24 28 21 18 22 28.33
DJ JessieB                            0 0 0 0 0 0 0 0.00';

preg_match('/Current at\s(\d+\/\d+\/\d+)/',$test,$date);
echo $date[1],"<hr />\n";
preg_match_all('/(.*)\s+(([0-9]+ ){7}[0-9\.]+)/',$test,$numbers);
$dj = $numbers[1];
foreach ($dj as $k => $v) $dj[$k] = trim($v);
$num = $numbers[2];
foreach ($num as $k => $v) $num[$k] = explode(' ',trim($v));
$data = array_combine($dj, $num);
echo '<pre>',print_r ($data),'</pre>';
?>

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.