Reputation: 289
I tried to get all the datas with a specific format (02:AA:13:09:45:DE)from a string using PHP (which may be stored in a CSV file) using the ereg
function in PHP.
This is what I tried:
$string = '02:AA:13:09:45:DE -90 hRm / -21 450 s RX: 1.0 , 1 . TX: 2.0 , MCS 0, 1 . 13:09:13:10:15:5D -33 hRm / -55 5000 s RX: 66.0 , MCS 0, 333 . TX: 66.0, MCS 0, 333 . 17:09:A3:07:30:DC -55 hRm / -22 hRm 456 s RX: 43.0 MCS 0, 434. TX: 43.0 , MCS 0, 43 .'
$pattern='^[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}';
ereg($pattern, $string, $matches);
print_r($matches);
This was giving only the first value: 02:AA:13:09:45:DE
. As I should not use preg_match_all
(or any other preg
functions), is there any other way to implement this (getting all thee datass)?
Upvotes: 0
Views: 795
Reputation: 3241
Well, preg_match
or preg_match_all
is the best option, and ereg is dead.
So your alternatives are somehow dirty.
$items = explode(" ", $string);
foreach($items as $item) {
$chunks = explode(":", $item);
// check current item. If consist of 6 hex digits separated by :
if (count($chunks) == 6) {
$valid = TRUE;
// all numbers must be hex!
foreach($chunks as $chunk) {
if (!ctype_xdigit($chunk)) {
$valid = FALSE;
break;
}
}
if ($valid) {
echo $item;
}
}
}
Upvotes: 2