-4

I have a string say - "abc12345xyz"

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

Another string is "pick up at 6:30 pm" How can split like...

0 => pick up
1 => at
2 => 6:30 pm
7
  • 1
    Regarding your second example (which you just edited in), you'll need to explain exactly on what basis that split should occur, cause it's quite unclear. Why would "pick up" and "at" be in different positions? Should this take into account natural language? Also please share some attempts. Commented Nov 20, 2019 at 7:01
  • Ok. Like this... "pickup at", "take out at", "takeout at", "pickup 6 pm", "pickup 6:30 pm", "pickup 6pm" Commented Nov 20, 2019 at 7:04
  • Not sure what you're attempting to say / mean. Commented Nov 20, 2019 at 7:06
  • The string can be like that(I mentioned in comment). Commented Nov 20, 2019 at 7:08
  • 1
    It seems you just changed the question entirely. Splitting abc12345xyz is nothing like splitting pick up at 6:30 pm, especially not if you want to include the pm with the time part... Commented Nov 20, 2019 at 7:16

1 Answer 1

4

You can use preg_split to split the string on a set of digits, using the PREG_SPLIT_DELIM_CAPTURE option to also capture the digits into the output array:

$string = "abc12345xyz";
$array = preg_split('/(\d+)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($array);

Output:

Array
(
    [0] => abc
    [1] => 12345
    [2] => xyz
)

Demo on 3v4l.org

Update

Based on the alterations to the question, it seems this regex might be more useful to split on:

\s*(\d+(?::\d+)?)\s*

as it also allows the "numeric" part to look like a time e.g. 6:30. You can use it like this:

$strings = array("abc12345xyz", "pick up at 6:30 pm", "pickup 6 pm", "pickup 6pm");
foreach ($strings as $string) {
    $array = preg_split('/\s*(\d+(?::\d+)?)\s*/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
    print_r($array);
}

Output:

Array
(
    [0] => abc
    [1] => 12345
    [2] => xyz
)
Array
(
    [0] => pick up at
    [1] => 6:30
    [2] => pm
)
Array
(
    [0] => pickup
    [1] => 6
    [2] => pm
)
Array
(
    [0] => pickup
    [1] => 6
    [2] => pm
)

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

5 Comments

@ArchitaPanchal please see my updated answer. This should get you close to what you want.
That doesn't work with his second example though ("pick up" and "at" at different positions). I'm guessing he needs some kind of NL stuff, or maybe a whitelist of possible actions in an array or something. But he doesn't seem to want to tell us so...
@Jeto yeah OPs second example is not doable without something significantly more sophisticated. But without input it's impossible to guess at what that might be. I just thought I would offer up something a little closer to the completely changed question requirements...
I support the closure of this Unclear question.
@mickmackusa perhaps better to just roll back to the original question and answer?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.