试试这个:
$str=array( "Chelsea 0-5 MAN-UNITED",
"Chelsea 0-2 Aston Villa",
"Chelsea 0-3 Pokemon (TRASH i dont want)");
$arr = array();
$pattern = "/([0-9]+\-[0-9]+)/";
foreach ($str as $sub) {
$parts = preg_split($pattern, $sub, -1, PREG_SPLIT_DELIM_CAPTURE);
if (count($parts) == 3) {
$team1 = trim(preg_replace("/\(.*\Z/", "", $parts[0]));
$score = trim($parts[1]);
$team2 = trim(preg_replace("/\(.*\Z/", "", $parts[2]));
$teams = "$team1 Vs $team2";
$element = array("teams" => $teams, "score" => $score);
$arr[] = $element;
}
}
print_r($arr);
输入:
$str=array( "Chelsea 0-5 MAN-UNITED",
"Chelsea 0-2 Aston Villa",
"Chelsea 0-3 Pokemon (TRASH i dont want)");
输出:
Array
(
[0] => Array
(
[teams] => Chelsea Vs MAN-UNITED
[score] => 0-5
)
[1] => Array
(
[teams] => Chelsea Vs Aston Villa
[score] => 0-2
)
[2] => Array
(
[teams] => Chelsea Vs Pokemon
[score] => 0-3
)
)
这是在行动:http ://eval.in/14236
哦,请确保您至少使用 PHP 4.0.5。就是在那个时候PREG_SPLIT_DELIM_CAPTURE
介绍的。