I need to get the 1st part of a string, the only consistencies would be that the string would start with at least 1 letter. If it contains numbers I just want the part before the 1st number.

eg.

AB1234 // need to return AB
A3A2N123 // need to return A
CB123Z // need to return CB
X32A123 // need to return X
CC892CCX384B // need to return CC
ZB // need to return ZB
Y // need to return ZB

Thanks

    Can't find an edit thread button.

    The last one should be: Y // needs to return Y

      
      function getBeforeFirstNumber($string)
      {
           for($i=0;$i<strlen($string);$i++)
           {
                if(is_numeric($string[$i]))
                {
                     return substr($string,0,$i);
                }
           }
      }
      echo getBeforeFirstNumber('CC892CCX384B');
      //RETURNS CC
      
        if (preg_match('#^[A-Z]+#', $string, $match))
        	echo $match[0];
        

          Both those functions had flaws for the data I was using as examples, but it they led me down the right path so thanks.

          I went with this in the end.

          function getBeforeFirstNumber($string){ //:String
          
          $ret = $string;
          $newstring = '';
          $array = str_split($string);
          
          foreach($array as $char) {
          
          	if(!is_numeric($char)){
          		$newstring[]=$char;
          	}else{
          		break;
          	}
          
          }
          
          if(is_array($newstring)){
          	$ret = join('',$newstring);
          }
          
          return $ret;
          
          } 
          
            louisl;10935358 wrote:

            Both those functions had flaws for the data I was using as examples, but it they led me down the right path so thanks.

            No, mine works perfectly with the example data.
            Data from the regexp:
            AB
            A
            CB
            X
            CC
            ZB
            Y

            And from your example
            AB
            A
            CB
            X
            CC
            ZB
            Y

            As for scupolous code, there were just two small issues that are easy to fix, and I still find his code more readable than yours. It is also more efficient. The two things missing is start condition and the other was if the whole string is ok.

            function getBeforeFirstNumber($string)
            {
            	if (is_numeric($string[0]))		// added
            		return false;			// added
                 for($i=0;$i<strlen($string);$i++)
                 {
                      if(is_numeric($string[$i]))
                      {
                           return substr($string,0,$i);
                      }
                 }
                 return $string;				// added
            }
            
              johanafm;10935359 wrote:

              As for scupolous code, there were just two small issues that are easy to fix, and I still find his code more readable than yours. It is also more efficient. The two things missing is start condition and the other was if the whole string is ok.

              I assumed based on his data set that those conditions wouldn't exist however checks and balances are good to have... Good catches 🙂

                So did I: assuming there won't be non alpha-numeric characters, since it would accept "A!#&#8364;" and return that string in full.

                And he did state that the string would start with a letter.

                  @: Apologies, your's does work fine for the posted examples, I just realised where it broke in my tests - lowercase data was messing with it, easy fix I know. either I can uppercase it or change the regex. (although I have to admit my regex knowledge is poor so I tend to avoid it where possible).

                  scrupul0us's code failed with the "Y" example but with your changes now works. I've realised that although ideal data is as I described it's possible there could be some junk in there ie numbers 1st "1XyJJK" for example so I needed to return the original string untouched if I dont get alphanumerical chars at the beginning... but that's more specific to my app and info you never had.

                  It just goes to show there's always more than one way to program, and again thanks to you both for your answers.

                    If you're interested in more reg exp specifics, you could either change the character class to [A-Za-z] to allow for lower case characters, or keep it as is but end it with #i instead of just #. the last part consists of pattern modifiers, and i means case Insensitive.

                      Write a Reply...