ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Need to split this string in PHP

    Scheduled Pinned Locked Moved Solved IT Discussion
    phpstringsplit
    15 Posts 4 Posters 1.7k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 1
      1337 @JaredBusch
      last edited by 1337

      @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
      623f32c1-ffe4-4063-ab32-9adf09aa88d2-image.png

      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

      JaredBuschJ 1 Reply Last reply Reply Quote 1
      • dafyreD
        dafyre
        last edited by

        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>";
        
        1 1 Reply Last reply Reply Quote 1
        • 1
          1337 @dafyre
          last edited by

          @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!

          dafyreD 1 Reply Last reply Reply Quote 1
          • dafyreD
            dafyre @1337
            last edited by

            @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.

            1 1 Reply Last reply Reply Quote 0
            • 1
              1337 @dafyre
              last edited by

              @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/

              1 Reply Last reply Reply Quote 2
              • 1
                1337
                last edited by 1337

                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.

                dafyreD 1 Reply Last reply Reply Quote 3
                • dafyreD
                  dafyre @1337
                  last edited by

                  @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!

                  1 Reply Last reply Reply Quote 0
                  • JaredBuschJ
                    JaredBusch @1337
                    last edited by

                    @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..

                    1 Reply Last reply Reply Quote 0
                    • JaredBuschJ
                      JaredBusch @JasGot
                      last edited by JaredBusch

                      @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

                      199bb6cd-16f7-40f9-aee9-3fa8e980e528-image.png

                      JaredBuschJ 1 Reply Last reply Reply Quote 1
                      • JaredBuschJ
                        JaredBusch @JaredBusch
                        last edited by

                        @jaredbusch said in Need to split this string in PHP:

                        This:
                        https://github.com/sorvani/freepbx-helper-scripts/commit/23ef9bd7aca3d791217aab86ddd53b30d7838563

                        And 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 🙂

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post