Php 将字符串拆分为数组,然后循环到HTML表中

Php 将字符串拆分为数组,然后循环到HTML表中,php,loops,Php,Loops,在下面的示例中,我从数据库中提取了一个字符串。我试图将字符串循环到一个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" ___________________________________________________

在下面的示例中,我从数据库中提取了一个字符串。我试图将字符串循环到一个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;
    } 
}


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

您的方法是..?请显示一些您尝试的代码。另外,您是否可以控制从数据库中提取的字符串?最好为列和行使用不同的分隔符。@AlmaDoMundo我已经用最新的失败更新了我的答案attempt@CMKanode我已经用我最近失败的尝试更新了我的答案。我试过了,但是我很难在需要的地方绑定它们。唯一的问题是,
BE Record
可以是任意两个字母,而不仅仅是
BE
编辑了我的答案。根据您的评论,您需要使用两个数组元素$dr[0]和$dr[1]来获得+2个存储。我试图运行代码,但数组与PHPFIDDLE混淆,这是因为重复了值20.24,但现在我已经更新了代码,希望不会再次出现。
$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.
?>