shaunie Posted February 7, 2013 Share Posted February 7, 2013 Hi, I have a string that will have zero or more letters followed by one or more numbers. For example aa1 a1 a12 b12 34 How can I split the sting so that I get just the letter part or the number part? So letters would return: aa a a b null and numbers would return: 1 1 12 12 34 Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 7, 2013 Share Posted February 7, 2013 I'd use Regular Expressions for something like this, as they lend themselves quite nicely for this kind of thing. (It is what they were made for, after all. ) Anyway, to construct the proper RegExp for this, we first need to start with the delimiter (it's what tells the RegExp function what's the actual expression, and what's flags/modifiers). Just about any non-alphanumeric character can be used, but the most common ones are # and /. Let's use / for this one: / Then we want to capture the next group, so that we get it into a variable of its own. We do this by starting a capturing sub-group: /( Next you wanted all of the letters, this is done by creating a character class, and defining the range of characters you want to match. Note that the range uses the order of the character[1] set in question, so you can get some unexpected results if you make a mistake. A character group is defined by using square brackets, and a range is defined by two characters separated by a dash. Also, since we wanted more than one character, we need to specify this by using * (zero or more): /([a-z]*) Now we've got all of the characters, if there are any, in group 1. I'll leave it up to you to figure out how to add numbers to the RegExp, and adding the closing delimiter. The way this is used you can read more about in the PHP Manual, more specifically in the page for preg_match (). [1]See the ASCII table for an example. PS: Moving to the correct section, since we're talking RegExps here. Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 7, 2013 Share Posted February 7, 2013 $strings = array(); // sample data $letters = array(); $numbers = array(); foreach($strings as $str){ if(preg_match("#([a-z]*)([0-9]+)#i", $str, $matches)){ $letters[] = $matches[1]; $numbers[] = $matches[2]; } } print_r($letters); print_r($numbers); Preg match is very handy, would be quicker to use on blocks of text rather than single elements in a loop. (for text though use preg_match_all, slight differences) Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 7, 2013 Share Posted February 7, 2013 In case you don't want to use regular expressions, you try something like the following: <?php $testVal = $_GET['testVal']; //<-- add value to test here; I just used a GET variable for easy testing $letters = array(); $numbers = array(); for($i=0; $i<strlen($testVal); $i++) { if(ctype_digit((string)$testVal[$i])) { $numbers[] = $testVal[$i]; } else { $letters[] = $testVal[$i]; } } print '<div>Letters: ' . implode('', $letters) . '</div>'; print '<div>Numbers: ' . implode('', $numbers) . '</div>'; ?> 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.