拆分数组值,这是提供数据的最佳途径?

时间:2013-10-25 14:25:48

标签: php algorithm php-5.3

我有这个数组(无法更改):

Array
(
    [0] => AGUI vs CARI Hora 7:30 pm</span></p>PUERTO LA CRUZ</span></p>
    [1] => MAGA vs LEON Hora 7:30 pm</span></p>CARACAS</span></p>
    [2] => BRAV vs TIGR Hora 7:30 pm</span></p>MARACAY</span></p>
    [3] => TIBU vs CARD Hora 7:30 pm</span></p>BARQUISIMETO</span></p>
)

我需要拆分以分别获取值,例如:

[0] => AGUI vs CARI Hora 7:30 pm</span></p>PUERTO LA CRUZ</span></p>

我需要分成:

AGUI vs CARI
7:30 pm (I not need the word "Hora")
PUERTO LA CRUZ

我尝试使用以下代码替换</span></p>

str_replace("</span></p>", "-", trim($the_string_var));

并清理更多的字符串,但它不起作用,可以给我一些这个算法的提示或想法吗?

2 个答案:

答案 0 :(得分:1)

我做得最好;)

<?php
$str="AGUI vs CARI Hora 7:30 pm</span></p>PUERTO LA CRUZ</span></p>";
$str=str_replace(array(' vs ',' Hora ','</span></p>'),'XXX',$str);
$arr=explode('XXX',$str);
foreach ($arr as $key=>$value)
    if (empty($value))
        unset($arr[$key]);
print_r($arr); //If you want it seperately, then access it like $arr[0],$arr[1],...

输出:

Array ( [0] => AGUI [1] => CARI [2] => 7:30 pm [3] => PUERTO LA CRUZ )

答案 1 :(得分:0)

试试这个: -

 $var =  AGUI vs CARI Hora 7:30 pm</span></p>PUERTO LA CRUZ</span></p>

 $fvar = explode('Hora',$var);
 $svar = explode('</span></p>',$fvar[1]);

 echo $fvar[0];   //contains AGUI vs CARI
 echo $svar[0];   //contains 7:30 pm
 echo $svar[1];   //PUERTO LA CRUZ