1

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.

2
  • explode(". ",$string ); Commented Nov 8, 2016 at 13:22
  • But the first array contains two dots(.) s Commented Nov 8, 2016 at 13:22

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. It worked. It counts numbers of characters. But I user print_r() . Thanks.
But the $matches[0] is additional.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.