Hi all,

I have a string which contains "abc123".

I need to split the alpha character from the numeric and I also know that I should be using "preg_match" to perform the operation.

I have looked at most of the posts concerning "preg_match" but can not see how I can complete this process.

Can any one please help.

    $str = 'abc123';
    preg_match('#([a-z]*)(\d*)#', $str, $match);
    echo 'alpha: ' . $match[1];
    echo 'num: ' . $match[2];?>
    

    The # is pattern delimiter. It marks start and end of the pattern, and you can choose any from a bunch of characters for this.

    [] denotes a character class. in this case, any characters maching the characters a through z. If you also want to get uppercase letters, you'd need to use either [a-zA-Z] or end the pattern with #i. The part after the ending delimiter is pattern modifier, and the i stands for case insensitive.

    \d matches a decimal digit.

    • matches one or more characters. So [a-z]* matches zero or more lower case letters.

    () marks a subpattern, which is capturing by default. (?🙂 marks a non capturing subpattern. The capturing subpatterns are put into $match[1], $match[n], where n is the number of capturing subpatterns. $match[0] holds the complete pattern match.

      Hi johanafm,

      Can you see any reason why the first script works and the second one does not;

      FIRST SCRIPT

      $_GET['flightinfo'] = "ba2691";
      $_GET['clientmobile'] = "11122211222";
      
      
      $flightinfo = $_GET['flightinfo'];
      preg_match('#([a-z]*)(\d*)#', $flightinfo, $match);
      
      $clientmobile = $_GET['clientmobile'];
      $_SESSION['flightinfo'] = $flightinfo;
      $_SESSION['airline'] = $match[1];; 
      $_SESSION['flightno'] = $match[2];;
      $_SESSION['mobile'] = $clientmobile;
      
      

      SECOND SCRIPT

      $flightinfo = $_GET['flightinfo'];
      $clientmobile = $_GET['clientmobile'];
      
      
      preg_match('#([a-z]*)(\d*)#', $flightinfo, $match);
      
      
      $_SESSION['flightinfo'] = $flightinfo;
      $_SESSION['airline'] = $match[1]; 
      $_SESSION['flightno'] = $match[2];
      $_SESSION['mobile'] = $clientmobile;
      
      

        the second script gets its values from the query string... the first script assigns them manually

          Hi scrupul0us,

          Thanks for your reply.

          The question was, why dos the first one work when it gets its values manually and the second script does not work when it get its values from a query string.

          If I echo out "$flightinfo = $_GET['flightinfo'];" should it not return the value.

          Many thanks again

            you need to pass values in the query string:

            index.php?flightinfo=ba2691&clientmobile=11122211222

              And also check if you have that info in the querystring or not

              if (!empty($_GET['flightinfo'])) {
              ...
              	// and when/if you want to use cell number to send sms
              	if (!empty(...)) {
              	}
              }

                I was just curious to see if this would work, and it did:

                <?php
                $flightinfo = 'ba1234';
                
                list($_SESSION['airline'], $_SESSION['flightno']) =
                      preg_split('#(?<=[a-z])(?=\d)#i', $flightinfo);
                
                echo "<pre>";print_r($_SESSION);echo "</pre>";
                

                (I'm not saying it's necessarily how I'd do it, but I find it sort of cool. 🙂 )

                  Interesting way to do it 🙂
                  I've never thought about splitting a string at the fictional space between two neighbouring characters.

                    Hi NogDog and all,

                    <?php
                    $flightinfo = 'ba1234';
                    
                    list($_SESSION['airline'], $_SESSION['flightno']) =
                          preg_split('#(?<=[a-z])(?=\d)#i', $flightinfo);
                    
                    echo "<pre>";print_r($_SESSION);echo "</pre>"; 
                    

                    NogDog, your solution works perfectly. Thank you.

                    May I take time time to wish you all a peaceful time over the coming Christmas and New Year.

                    Again many thanks.

                      johanafm;10936996 wrote:

                      Interesting way to do it 🙂
                      I've never thought about splitting a string at the fictional space between two neighbouring characters.

                      What nog is actually doing is splitting using positive lookbehind and lookahead assertions. Assertions don't actually consume text, but rather match positions between characters within the string instead (also known as zero width assertions). So he's matching the position after the last letter and before the first number, and splitting off of that.

                      All in all, it is a pretty slick solution nog! 🙂

                      Alternatively, given that we're following a format, perhaps one can also make use of sscanf():

                      $flightinfo = 'Ba1234';
                      sscanf($flightinfo, '%[a-zA-Z]%d', $_SESSION['airline'], $_SESSION['flightno']);
                      echo "{$_SESSION['airline']}<br />{$_SESSION['flightno']}";
                      

                      Many ways to skin a cat, I suppose.

                        Write a Reply...