RabPHP Posted February 20, 2009 Share Posted February 20, 2009 Greetings, I have a variable that looks like "THISISSOME(2 EXTREMELY)RANDOMDATA12345AP" I need to strip out the random data and return everything after the first occurance of a number that is not inside a parenthesis. In this example, I want to only get 12345AP. The data may be random wordsand characters without a defined length. This one has me baffled but I know it's possible, please advise! Thanks Rab Quote Link to comment Share on other sites More sharing options...
farkewie Posted February 20, 2009 Share Posted February 20, 2009 s <?php $string = "THISISSOME(2 EXTREMELY)RANDOMDATA12345AP"; //get rid of everything before the ")" $string = explode( ')', $string ); //Keep everything after the last ")" $string = $string[count( $string ) - 1]; $strLen = strlen( $string ); $index = 0; for ( $i = 0; $i < $strLen; $i++ ) { if ( is_numeric($string[$i]) ) { //Grab where the numbers start $index = $i; //stop any more checks. $i = $strLen + 1; } } //grab the string where the numbers start $string = substr( $string, $index ); print $string; // outputs 12345AP ?> Quote Link to comment Share on other sites More sharing options...
corbin Posted February 20, 2009 Share Posted February 20, 2009 Depending on the complexity of your string, this might work a little better: if(preg_match('~\([^\(]+\)([\d]+)~', $str, $m)) { echo $m[1]; } Quote Link to comment Share on other sites More sharing options...
RabPHP Posted February 20, 2009 Author Share Posted February 20, 2009 Ok one more final addition. Sometimes there is a number outside of the parenthesis, it's always either a 1 or a 2. How do I strip off the first character if it's a 1 or 2? Rab Quote Link to comment Share on other sites More sharing options...
farkewie Posted February 20, 2009 Share Posted February 20, 2009 Is the number the next char after the parenthesis? Your example string has a first number of a 1 if we were to srip the fist 1 you would lose part of your string. Quote Link to comment Share on other sites More sharing options...
RabPHP Posted February 20, 2009 Author Share Posted February 20, 2009 Those portions of the string contining a 1 or a 2 are uniform, I simple did a str_replace before subjecting your code on the string and it resolved the problem. Much appreciated! Rab Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.