I have a string extracted from a database in the example below. I'm trying to loop through the string into a HTML table to display the results as in the diagram. The string can vary in length but will always follow the same format.
$string = "18/05-01/06 01/06-06/07 06/07-22/08 22/08-14/09 DR Record + 2 21.47 20.24 27.15 20.24 BE Record + 2 24.05 22.68"
_______________________________________________________________________
|18/05-01/06 | 01/06-06/07 | 06/07-22/08 | 22/08-14/09 |
_______________________________________________________________________
DR Record + 2 | 21.47 | 20.24 | 27.15 | 20.24 |
_______________________________________________________________________
BE Record + 2 | 24.05 | 22.68 | | |
_______________________________________________________________________
Everything i've tried doesn't seem to work. Any ideas would be greatly appreciated.
This is what i've tried so far
$string = "18/05-01/06 01/06-06/07 06/07-22/08 22/08-14/09 DR Record + 2 21.47 20.24 27.15 20.24 BE Record + 2 24.05 22.68"
$parts = preg_split('/\s+/', $string );
$date = array();
$record = array();
$price = array();
foreach($parts as $part) {
// check date
if(strpos($part,'-') !== false) {
$date[] = $part;
}
// check price
elseif(strpos($part, '+') !== false) {
$record[] = $part;
}
// echeck record
elseif(strpos($part, '.') !== false) {
$price[] = $part;
}
}
$parts = preg_split('/\s+/', $string ); //it returns an array
<?php
function gsb($string, $start, $end)
{
$string = " " . $string;
$ini = strpos($string, $start);
if ($ini == 0)
return "";
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$s= "18/05-01/06 01/06-06/07 06/07-22/08 22/08-14/09 DR Record + 2 21.47 20.24 27.15 20.24 BE Record + 2 24.05 22.68";
$s1=explode(" ",gsb("*".$s,"*"," DR"));//'*' is just used to add an char at first so that we can extract string between it.
$dr=explode(" ",gsb($s," DR Record "," BE Record"));
$be=explode(" ",gsb($s.'*',$dr[count($dr)-2].' '.$dr[count($dr)-1].' ',"*"));//Edited according to comment and you need to access values from $be[2] as $be[0]$$be[1] will contain those two words.
?>
Now you have 3 arrays $s1
,$dr
& $be
containing all the values of table you need now just use it according to your requirments.