0

在下面的示例中,我从数据库中提取了一个字符串。我正在尝试将字符串循环到 HTML 表中,以显示如图所示的结果。字符串的长度可以不同,但​​始终遵循相同的格式。

$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       |             |             |
_______________________________________________________________________

我尝试过的一切似乎都不起作用。任何想法将不胜感激。

这是我迄今为止尝试过的

$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;
    } 
}
4

2 回答 2

1
$parts = preg_split('/\s+/', $string ); //it returns an array
于 2013-09-24T18:02:10.113 回答
0
<?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.
?>

现在您有 3 个数组$s1$dr&$be包含您现在需要的所有表值,只需根据您的要求使用它即可。

于 2013-09-24T18:19:24.410 回答