2017-10-07 102 views
-1
$str = "SC - ESV Parndorf 2 - 5 SV Horn"; 
$str4 = explode(" - ", $str,2); 
$str5=$str4[0];  
$str6 = explode(" ", $str5); 
$Num=end($str6);   
$str7=$str4[1];  
$str8 = explode(" ", $str7); 
$Num1 = $str8[0]; 

如果我有两个“ - ”,则无法将数字2和5取出。无法使用包含两个连字符的爆炸分割

+0

欢迎stackoverflow.com!请阅读[如何提问](https://stackoverflow.com/questions/how-to-ask),[如何创建一个最小,完整和可验证的示例](https://stackoverflow.com/help/mcve ),然后相应地编辑您的问题。您可能还想查看网站,了解更多关于如何在这里工作的信息。 – wp78de

+0

我想提取分数。 –

+0

答案很有帮助,但是......“感谢您的反馈!记录下那些名声不到15的人的投票记录,但不要更改公开显示的帖子分数。” –

回答

0

我建议使用正则表达式来替代,例如(^.+) (\d+) - (\d+) (.+$)
preg_match_all()这样一起:

$re = '/(^.+) (\d+) - (\d+) (.+$)/'; 
$str = 'SC - ESV Parndorf 2 - 5 SV Horn'; 
preg_match_all($re, $str, $matches); 
foreach ($matches as $match) { 
    echo $match[0] . "\n"; 
} 

根据你的问题,你最感兴趣的捕获组2和3,RESP。 $matches[2][0]$matches[3][0]

+0

两个建议函数的区别在于:'preg_match'在第一场比赛后停止。 'preg_match_all'继续查找,直到完成处理整个字符串。如果你有一个匹配preg_match的单个字符串就足够了,否则preg_match_all会更好。 http://php.net/manual/en/function.preg-match-all.php – wp78de

0

对于这样的工作,你最好使用preg_match

$re = '/(\d+)\s+-\s+(\d+)/'; 
$str = 'SC - ESV Parndorf 2 - 5 SV Horn'; 
preg_match($re, $str, $matches); 
print_r($matches); 

说明:

/   : regex delimiter 
    (\d+) : group 1, 1 or more digits 
    \s+-\s+ : a dash between some spaces 
    (\d+) : group 2, 1 or more digits 
/   : regex delimiter 

输出:

Array 
(
    [0] => 2 - 5 
    [1] => 2 
    [2] => 5 
) 
+0

@ wp78de:并非如此,您正在使用preg_match_all,并且您正在捕获太多的群组 – Toto