-1

I have a string like this:

$str="hello world 2 in 365 php";

I want this:

['hello world' , 'in' , 'php']

Any ideas?

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
lasan
  • 365
  • 2
  • 5
  • 13
  • Please see [ask] and [mcve]. Follow those guidelines. And show what you've tried so far and where you got stuck – Alex Tartan Jul 02 '17 at 21:58
  • Possible duplicate of [Split String into Text and Number](https://stackoverflow.com/questions/14348018/split-string-into-text-and-number) – Frank M Jul 02 '17 at 21:59
  • Alternatively you can use [preg_split](http://php.net/manual/en/function.preg-split.php) – Farrukh Ayyaz Jul 02 '17 at 22:05

2 Answers2

5

I was bored:

$result = preg_split('/ ?\d+ ?/', $str);

Split on an optional space ? followed by 1 or more digits \d+ followed by an optional space ?. Just remove the ?s if you want the spaces to be required.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

That would do the work for you (I've split into multiple variables just to make sure you understand the way I've done that)

$str = "hello world 2 in 365 php";
$withoutNum = preg_replace('/[0-9]+/', '', $str);
$removedDoubleWhiteSpaces = preg_replace('/\s+/', ' ', $withoutNum);

$splitedArr = explode(' ', $removedDoubleWhiteSpaces);

var_dump($splitedArr);

Edit:

After I've read again, the solution which offered above of my solution is the ideal one. Sorry.

MyLibary
  • 1,763
  • 10
  • 17