Pasquale Crea Pasquale Crea - 5 months ago 11
PHP Question

Explode string at any interger and delete the interger

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.

Answer
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) ); 
Comments