PHP split string into integer element and string

29,117

Solution 1

You can use preg_split using lookahead and lookbehind:

print_r(preg_split('#(?<=\d)(?=[a-z])#i', "0982asdlkj"));

prints

Array
(
    [0] => 0982
    [1] => asdlkj
)

This only works if the letter part really only contains letters and no digits.

Update:

Just to clarify what is going on here:

The regular expressions looks at every position and if a digit is before that position ((?<=\d)) and a letter after it ((?=[a-z])), then it matches and the string gets split at this position. The whole thing is case-insensitive (i).

Solution 2

Use preg_match() with a regular expression of (\d+)([a-zA-Z]+). If you want to limit the number of digits to 1-4 and letters to 6-9, change it to (\d+{1,4})([a-zA-Z]{6,9}).

preg_match("/(\\d+)([a-zA-Z]+)/", "0982asdlkj", $matches);
print("Integer component: " . $matches[1] . "\n");
print("Letter component: " . $matches[2] . "\n");

Outputs:

Integer component: 0982
Letter component: asdlkj

http://ideone.com/SKtKs

Solution 3

You can also do it using preg_split by splitting your input at the point which between the digits and the letters:

list($num,$alpha) = preg_split('/(?<=\d)(?=[a-z]+)/i',$Order_num);

Solution 4

You can use a regex for that.

preg_match('/(\d{1,4})([a-z]+)/i', $str, $matches);
array_shift($matches);
list($num, $alpha) = $matches;
Share:
29,117

Related videos on Youtube

David19801
Author by

David19801

Updated on July 09, 2022

Comments

  • David19801
    David19801 almost 2 years

    I have a string say: Order_num = "0982asdlkj"

    How can I split that into the 2 variables, with the number element and then another variable with the letter element in php?

    The number element can be any length from 1 to 4 say and the letter element fills the rest to make every order_num 10 characters long in total.

    I have found the php explode function...but don't know how to make it in my case because the number of numbers is between 1 and 4 and the letters are random after that, so no way to split at a particular letter. Please help as specifically as possible!

  • BoltClock
    BoltClock over 13 years
    So that's how you use lookaround assertions with preg_split()! +1
  • moinudin
    moinudin over 13 years
    What's the advantage of using split over match?
  • moinudin
    moinudin over 13 years
    You need to escape the \ in \d.
  • Felix Kling
    Felix Kling over 13 years
    @marcog: Good question... I was just focused on splitting the string ;) I'm not sure whether there is any advantage.
  • Felix Kling
    Felix Kling over 13 years
    @marcog: No I don't, it works fine for me, even in double quotes, but to be sure, I will put it in single quotes.
  • BoltClock
    BoltClock over 13 years
    @marcog: I think either way is fine, as Felix says it depends on your own way of thinking :)
  • Your Common Sense
    Your Common Sense over 13 years
    @marcog that's the way stackoverflow works. They actually do answer the question without understanding it. They even barely read it. You know, the fastest answer will more likely be accepted.
  • Your Common Sense
    Your Common Sense over 13 years
    I won't use strict limit on the number but beside that it's most sensible answer.
  • Felix Kling
    Felix Kling over 13 years
    @Col. Shrapnel: There is not always the only true answer to a problem. People think in different ways. And knowing different ways to solve a problem is even better!
  • codaddict
    codaddict over 13 years
    $matches = array(); is not really needed. preg_match will ensure that the array will alway exist, even when there is no match.
  • codaddict
    codaddict over 13 years
    Also you need not escape \ in \\d+. \d+ is enough.
  • Felix Kling
    Felix Kling over 13 years
    @codaddict: But it is good coding style and easier to understand for the less proficient PHP programmer IMO.
  • Your Common Sense
    Your Common Sense over 13 years
    sure. but this noob will use your code as a magic chant, without slightest understanding. While straight preg_match would be way more readable and one can start learning regexp from this
  • Felix Kling
    Felix Kling over 13 years
    @Col. Shrapnel: And why can't someone learn regular expressions from lookarounds? Is it too advanced? Should we only give "simple" solutions? Btw. I'm the only one who actually linked to regex documentation so don't tell me that.
  • moinudin
    moinudin over 13 years
    @Felix If he's struggling to split a string like he is, then yes simpler is better.
  • Your Common Sense
    Your Common Sense over 13 years
    Yes, it's too advanced. Your solution is okay, I just hate it's being accepted and the way you took it - literally. Literal answers often do more harm than help.
  • moinudin
    moinudin over 13 years
    @codaddict Didn't know about the array(), but I don't like not escaping.
  • Felix Kling
    Felix Kling over 13 years
    @marcog: Of course simple solutions should be preferred. But what is simple? Is this code complicated? IMHO no! Is the expression complicated? Depends. It does not use quantifiers, it just matches one position and two characters. But why don't make use it if it is there (lookaround)?
  • moinudin
    moinudin over 13 years
    @Felix #(?<=\d)(?=[a-z])#i vs /(\\d+)([a-zA-Z]+)/: go back to your first week of exploring regular expressions, which do you think you'd understand first?
  • Felix Kling
    Felix Kling over 13 years
    @marcog: Without any explanation probably none of these.
  • moinudin
    moinudin over 13 years
    @Felix emphasis on first, i.e. you're learning regular expressions.
  • Felix Kling
    Felix Kling over 13 years
    The discussion is over anyway. All I wanted is to show one way of doing it. It is not my fault that the OP accepted the answer. If some of you guys want to have the-only-one-and-true-solution then go and live in a Brave new World. Don't forget that the answers are not only for the person who asks but also for those who come after...
  • Your Common Sense
    Your Common Sense over 13 years
    Upgrade your PHP from the ancient version to something modern and run it again
  • Felix Kling
    Felix Kling over 13 years
    @Wazzy: Because people are crazy and downvote answers they don't like. To be fair, your solution is not very elegant, but it gives the right solution. I would not upvote it, but I would not downvote either. +1 from my to compensate.
  • David19801
    David19801 over 13 years
    Hello, I am the OP. I liked this answer because it was simple and solved my problem. and because it SOLVED my problem, I know more about regex now, rather than reading pages and pages of waffle without learning a thing as is usual on SO. Thanks to FK, and col. get off the high horse. Good day to you all Gentlemen.
  • Felix Kling
    Felix Kling over 13 years
    @Col. Shrapnel: Works fine with PHP 5.3.3
  • Felix Kling
    Felix Kling over 13 years
    @Col. Shrapnel: So split is deprecated in PHP 5.3 and can be replaced by preg_split... it still with split or preg_split it works. I'm against downvoting without explanation and Upgrade your PHP from the ancient version to something modern and run it again is not an explanation.
  • Your Common Sense
    Your Common Sense over 13 years
    @David19801 good. What would be the pattern if you have to get 2 numbers out of string '1234werg3456'?
  • David19801
    David19801 over 13 years
    You'd do it once on the raw code, then flip the regex and do it again on array[1].
  • marcovtwout
    marcovtwout about 10 years
    Or use this even simpler expression: /(\d+)(.*)/
  • low_rents
    low_rents almost 8 years
    exactly what I needed 6 years after you posted your answer!