Jote
Jote

Reputation: 76

Splitting strings in to array by intergers in the string using php

I have this example string.

$string = "There four of them. These are: 1 The first one. Its is the most common. 2 The second one. 3 The third one. 4 This is the last.";

I wanted to split into array containing information given in the above $string. I want it to look like this.

Array ( 
   [0] => The first one. Its is the most common. 
   [1] => The second one.
   [2] => The third one.
   [3] => This is the last.
) 

Can any help me with it. Thank you.

Upvotes: 1

Views: 49

Answers (2)

Mohammad
Mohammad

Reputation: 21489

You can use regex in preg_match_all() to select target part of string. The regex select string between two digits.

preg_match_all("/\d+([^\d]+)/", $string, $matches);
print_r($matches[1]);

See result in demo

Upvotes: 1

phpchap
phpchap

Reputation: 382

You can use preg_split to split the string by integers, for example:

$string = "There four of them. These are: 1 The first one. Its is the most common. 2 The second one. 3 The third one. 4 This is the last.";

$matches = preg_split('/\d+/', $string);

var_dump($matches);

Upvotes: 3

Related Questions