Convert number sequence to array in PHP
arrays, php, preg-match-all, regex, string
Solution
if(preg_match_all('/.*?(?:\d+@){2}(\d+)@;/',$s,$m)) {
print_r($m[1]);
}
http://ideone.com/99M9t
or
You can do it 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
Problem
Kinda of a noobie in PHP and Regex, I receive the following from a web service: ``` test:002005@1111@333333@;10205@2000@666666@;002005@1111@55555@; ``` The above line is a sequence of 3 numbers which repeats 3 times. I would like to get the 3rd number of each sequence and I believe the best course (besides 3000 explodes) would be preg_match_all but I am having a tough time wrapping my mind around RegEx. The end result should look like this: ``` Array ( [0] => 333333 [1] => 666666 [2] => 55555 ) ``` Thanks in advance for any help.