Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/274154-split-a-string/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/274154-split-a-string/#findComment-1410739
Share on other sites

$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)

Link to comment
https://forums.phpfreaks.com/topic/274154-split-a-string/#findComment-1410740
Share on other sites

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>';
?>

Link to comment
https://forums.phpfreaks.com/topic/274154-split-a-string/#findComment-1410750
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.