Converting a numeric sequence to an array in PHP

Noobie stealth in PHP and Regex, I get the following from a web service:

test:002005@1111@333333@;10205@2000@666666@;002005@1111@55555@;

The above line is a sequence of three numbers, which is repeated 3 times. I would like to get the 3rd number of each sequence, and I believe that the best course (besides 3000 explosions) would be preg_match_all, but it's hard for me to spend time around RegEx.

The end result should look like this:

 Array
    (
        [0] => 333333
        [1] => 666666
        [2] => 55555
    )

Thanks in advance for your help.

+3
source share
5 answers
if(preg_match_all('/.*?(?:\d+@){2}(\d+)@;/',$s,$m)) {
        print_r($m[1]);
}

http://ideone.com/99M9t

or

You can do this using explode as:

$input = rtrim($input,';');
$temp1 = explode(';',$input);
foreach($temp1 as $val1) {
        $temp2 = explode('@',$val1);
        $result[] = $temp2[2];
}
print_r($result);

http://ideone.com/VH29g

+5
source

Use the explode () function

<?php

$pizza  = "piece1@piece2@piece3@piece4@piece5@piece6";
$pieces = explode("@", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

?>
+2

preg_match_all , :

$a = "test:002005@1111@333333@;10205@2000@666666@;002005@1111@55555@;";

preg_match_all('/@(\d+)@;/', $a, $m);

print_r($m);

$m [1] .

: http://php.net/manual/en/function.preg-match-all.php

0

, , ...

"You have a problem and you decided to use regular expressions ... now you have two problems."

Your problem can be easily solved if we assume that "test:" is not part of the actual string to be analyzed.

<?php

$in = '002005@1111@333333@;10205@2000@666666@;002005@1111@55555@;';

function splitGroupsAndGetColumn($input, $groupSeparator, $columnSeparator, $columnIndex, $skipEmpty=true)
{
    $result = array();

    $groups = explode($groupSeparator, $input);
    foreach($groups as $group)
    {
        $columns = explode($columnSeparator, $group);
        if (isset($columns[$columnIndex]))
        {
            array_push($result, $columns[$columnIndex]);
        }
        else if (! $skipEmpty)
        {
            array_push($result, NULL);
        }
    }

    return $result;
}

var_dump(splitGroupsAndGetColumn($in, ';', '@', 2));

Conclusion:

array(3) {
  [0]=>
  string(6) "333333"
  [1]=>
  string(6) "666666"
  [2]=>
  string(5) "55555"
}
0
source

My version:)

The regular expression (\ d +) means that I want everything that is a number one or more

php > $a = '002005@1111@333333@;10205@2000@666666@;002005@1111@55555@';
php > preg_match_all('/(\d+)/',$a,$matches);
php > var_dump($matches);
array(2) {
  [0]=>
  array(9) {
    [0]=>
    string(6) "002005"
    [1]=>
    string(4) "1111"
    [2]=>
    string(6) "333333"
    [3]=>
    string(5) "10205"
    [4]=>
    string(4) "2000"
    [5]=>
    string(6) "666666"
    [6]=>
    string(6) "002005"
    [7]=>
    string(4) "1111"
    [8]=>
    string(5) "55555"
  }
  [1]=>
  array(9) {
    [0]=>
    string(6) "002005"
    [1]=>
    string(4) "1111"
    [2]=>
    string(6) "333333"
    [3]=>
    string(5) "10205"
    [4]=>
    string(4) "2000"
    [5]=>
    string(6) "666666"
    [6]=>
    string(6) "002005"
    [7]=>
    string(4) "1111"
    [8]=>
    string(5) "55555"
  }
}
0
source

All Articles