If you're good with regular expressions you could use a loop of preg_replace instead and not hardcode the parsing in php. It would be more flexible and you could even put the regexp definitions in it's own file.
For instance with the expression | substitution:
^(Yealink)[^T]+(T[[0-9A-Z]*) ([0-9\.]+)$ | $1,$2,$3
^snom(.*)\/([0-9\.]+)$ | Snom,$1,$2
etc
User agent is reformatted into:
Yealink SIP-T54W 96.85.0.5 => Yealink,T54W,96.85.0.5
Yealink SIP VP-T49G 51.80.0.100 => Yealink,T49G,51.80.0.100
snomPA1/8.7.3.19 => Snom,PA1,8.7.3.19"
It's just easier to keep the result in a string and then separate it when you need it. You could use /
or tab or whatever as a separator. Then you use explode
if you want an array. In many cases it better to put the result into variables instead:
// User agent has been formatted into $s
$s='Snom,PA1,8.7.3.19';
// we put the result in variables
list($brand, $model, $firmware) = explode(',', $s);
// print the results
print "Brand: $brand | Model: $model | Firmware: $firmware\n";
Also be very careful when you're programming php and do not use "
for strings unless you have variables or escaped characters inside the string that needs to be interpreted. Use '
as your go to for strings. For instance 'test\r'
is not at all the same string as as "test\r"
. You got lucky in your sample script because "\s"
is an escape sequence in php but it's not a valid one so php didn't interpret it for you. But it's easy to run into conflicts between php and regular expressions when you encapsulate the strings with "
.