1

Example:

$string = "Apple0Bannana0Bannana1Cherry0";

What I need from said string:

Array(
[0] => Apple
[1] => Banana
[2] => Banana
[3] => Cherry
)

Something likely using preg_replace and/or explode, but I am not quite sure.

Problem is, there could be any int there between 0 and 49, so I am not sure how to get it to explode dynamically.

2
  • I think by explode you mean "split". If you do some searching for the php split function this may help. Commented Jun 17, 2016 at 1:35
  • Unfortunately split has been removed as of PHP 7, but I did look into that a bit as well. Commented Jun 17, 2016 at 1:39

1 Answer 1

3
var_dump( preg_split('~\d+~', preg_replace('~^\d+|\d+$~', '', $string)) );

First remove numbers at the start and end of the string, then split by one or more digits.

Thanks @Rizier123 for the hint for improvement:

var_dump( preg_split('~\d+~', $string, null, PREG_SPLIT_NO_EMPTY) ); 
Sign up to request clarification or add additional context in comments.

3 Comments

Take a look at the fourth parameter of preg_split()
Thanks @Rizier123 ! Added your hint.
This worked perfectly! Thank you so much! Quasimodo and Rizier!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.