我有一个这样的字符串
4-1,4-1,,,1-1,,5-4,2-1,
我需要提取每对中的第一个数字。例如, ( 4 -1, 4 -1,,, 1 - 1,, 5 - 4, 2 -1 ,)
有谁知道我怎么能做到这一点?
我有一个这样的字符串
4-1,4-1,,,1-1,,5-4,2-1,
我需要提取每对中的第一个数字。例如, ( 4 -1, 4 -1,,, 1 - 1,, 5 - 4, 2 -1 ,)
有谁知道我怎么能做到这一点?
$string = '4-1,4-1,,,1-1,,5-4,2-1,';
preg_match_all('/(\d+)-/', $string, $matches);
// search for 1 or more digits that are followed by a hyphen, and return all matches
print_r($matches[1]);
输出:
Array
(
[0] => 4
[1] => 4
[2] => 1
[3] => 5
[4] => 2
)
$couples = explode(",", $data);
$values = Array();
foreach ($couples as $couple)
{
if ($couple != "")
{
$split = explode("-", $couple);
$values[] = $split[0];
}
}