2

私はこのような文字列を持っています

4-1,4-1,,,1-1,,5-4,2-1,

そして、各ペアの最初の数字を抽出する必要があります。たとえば、( 4 -1, 4 -1,,, 1 - 1,, 5 - 4, 2 -1 ,)

どうすればこれを達成できるか知っている人はいますか?

4

2 に答える 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
)
于 2013-01-21T03:27:37.287 に答える
1
$couples = explode(",", $data);
$values = Array();

foreach ($couples as $couple)
{
    if ($couple != "")
    {
        $split = explode("-", $couple);
        $values[] = $split[0];
    }
}
于 2013-01-21T03:29:01.163 に答える