2013-09-24 44 views
0

我已经从在下面的例子一数据库中提取的字符串。我试图通过字符串循环到HTML表格中以显示结果,如图所示。字符串的长度可能会有所不同,但会始终遵循相同的格式。字符串分割到数组然后循环到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; 
    } 
} 
+0

而且你的做法是......? –

+0

请显示您尝试过的一些代码。另外,你是否可以控制从数据库中提取的字符串?对于列和行有不同的分隔符会很好。 –

+0

@AlmaDoMundo我已经更新了我的答案和我的最新尝试失败 – StevenPHP

回答

1
$parts = preg_split('/\s+/', $string); //it returns an array 
+0

感谢shuvo。我试过,但在需要事后 – StevenPHP

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包含表中的所有值,你现在需要根据只是用它你的需求。

+0

感谢我很难约束他们,唯一的问题是,'BE Record'可以是任意两个字母不只是'BE' – StevenPHP

+0

根据您的评论 – 2013-09-24 18:36:28

+0

编辑我的回答您需要使用两个数组元素$ dr [0]&$ dr [1]获得+2 – 2013-09-24 18:41:13