Solved Need to split this string in PHP
-
@jasgot said in Need to split this string in PHP:
@jaredbusch said in Need to split this string in PHP:
I have this bit of information.
"Jitsi2.10.5550Windows 10"
I need to split it into
$brand = "Jitsi"; $model = "Windows 10"; $firmware = "2.10.5550";
Jitsi is fixed, so easy to substring.
But the model and firmware not so much for me this morning.
I don't know what you are working on, so this may not work, but if you knew all known firmwares, you could put them in an array and then when you have a hit, all that remains is the OS.
it is a user agent sent by a sip register
-
@jaredbusch Is the Jitsi version always the same length? If so you could do it with some
substr()
magic. -
@dafyre said in Need to split this string in PHP:
@jaredbusch Is the Jitsi version always the same length? If so you could do it with some
substr()
magic.Probably not, version 12.10.5550 is one char longer. (Assuming they will get to bigger versions someday.)
-
@jaredbusch said in Need to split this string in PHP:
@jasgot said in Need to split this string in PHP:
@jaredbusch said in Need to split this string in PHP:
I have this bit of information.
"Jitsi2.10.5550Windows 10"
I need to split it into
$brand = "Jitsi"; $model = "Windows 10"; $firmware = "2.10.5550";
Jitsi is fixed, so easy to substring.
But the model and firmware not so much for me this morning.
I don't know what you are working on, so this may not work, but if you knew all known firmwares, you could put them in an array and then when you have a hit, all that remains is the OS.
it is a user agent sent by a sip register
Use a regular expression to split it.
The first group is letters, second is numbers and dots (perhaps some
-
and/
as well), third group start with letters and ends at the end of the string.Use this function:
preg_match($regex, $string, $result)
https://www.php.net/manual/en/function.preg-match.php -
If the preg_match stuff is too aggravating, I have a way that might work.
It's ugly and hacky, but I tested it with two random strings and it seems to format like you want it...
It returns an array.
<?php function parseUA($string) { $arrString=str_split($string); $model=substr($string,0,5); $i=0; $modStart=0; $modStop=0; $fwStart=0; $fwStop=0; $brandStart=0; $brandStop=0; $prevChar=""; $model=""; $brand=""; $firmware=""; while ($i < strlen($string)) { if ($i==0) { $curChar=current($arrString); } elseif ($i < strlen($string)) { $prevChar=current($arrString); $curChar=next($arrString); if (is_string($prevChar) && is_numeric($curChar) && $model=="") { $model=substr($string,0,$i); $fwStart=$i; } //echo "Starting Firmware: $firmware<br>"; if ($model<>"" && $curChar<>"." ) { if (is_numeric($prevChar)==true && is_numeric($curChar)==false) { $fwStop=$i; $brand=substr($string,$i,strlen($string)-$fwStop*-1); $firmware=substr($string,$fwStart,-strlen($brand)); } } } //echo "$i <br>"; $i++; } return array($brand,$firmware,$model); } $myString="HatsOff9.10.5Crash Tester"; //$myString="Jitsi2.10.5550Windows 10"; $myData=parseUA($myString); //print_r($myData); $model=$myData[2]; $firmware=$myData[1]; $brand=$myData[0]; echo "Model: $model"; echo "<br>"; echo "Brand: $brand"; echo "<br>"; echo "Firmware: $firmware"; echo "<br>";
-
@dafyre said in Need to split this string in PHP:
If the preg_match stuff is too aggravating, I have a way that might work.
It's ugly and hacky, but I tested it with two random strings and it seems to format like you want it...
It returns an array.
I'm impressed by the effort!
-
@pete-s said in Need to split this string in PHP:
@dafyre said in Need to split this string in PHP:
If the preg_match stuff is too aggravating, I have a way that might work.
It's ugly and hacky, but I tested it with two random strings and it seems to format like you want it...
It returns an array.
I'm impressed by the effort!
Some of us do not get along with regex, lol.
-
@dafyre said in Need to split this string in PHP:
@pete-s said in Need to split this string in PHP:
@dafyre said in Need to split this string in PHP:
If the preg_match stuff is too aggravating, I have a way that might work.
It's ugly and hacky, but I tested it with two random strings and it seems to format like you want it...
It returns an array.
I'm impressed by the effort!
Some of us do not get along with regex, lol.
I cheat...always. I try it out with something like https://regex101.com/
-
Try this:
https://regex101.com/r/Mv2Wlc/1
It's very educational if you hover over the different parts of the regexp.To use the regexp in php to split the string:
$s='Jitsi2.10.5550Windows 10'; $regexp='/(\D+)([\d\.]+)(\D+.*)/'; preg_match($regexp, $s, $result); print_r($result);
Results in this output:
Array ( [0] => Jitsi2.10.5550Windows 10 [1] => Jitsi [2] => 2.10.5550 [3] => Windows 10 )
Then use for example
$result[1]
for the brand. -
@pete-s said in Need to split this string in PHP:
Try this:
https://regex101.com/r/Mv2Wlc/1
It's very educational if you hover over the different parts of the regexp.To use the regexp in php to split the string:
$s='Jitsi2.10.5550Windows 10'; $regexp='/(\D+)([\d\.]+)(\D+.*)/'; preg_match($regexp, $s, $result); print_r($result);
Results in this output:
Array ( [0] => Jitsi2.10.5550Windows 10 [1] => Jitsi [2] => 2.10.5550 [3] => Windows 10 )
Then use for example
$result[1]
for the brand.Thanks for that!
-
@pete-s said in Need to split this string in PHP:
@jaredbusch said in Need to split this string in PHP:
Use a regular expression to split it.I always post here because, someone will almost always post a regex that is either correct or damned close before I can get back to things..
-
@jasgot said in Need to split this string in PHP:
@jaredbusch said in Need to split this string in PHP:
I don't know what you are working on,This:
https://github.com/sorvani/freepbx-helper-scripts/commit/23ef9bd7aca3d791217aab86ddd53b30d7838563 -
@jaredbusch said in Need to split this string in PHP:
This:
https://github.com/sorvani/freepbx-helper-scripts/commit/23ef9bd7aca3d791217aab86ddd53b30d7838563And yes @Pete-S or @JasGot whichever one of you always yells at me to not use
"
I know I need to clean that code up more