user3626180
user3626180

Reputation: 57

Split string without removing the delemiter

I need to split a string using:

$words = preg_split("/[\s,<>123456789]+/", $message);

but if I do that I end up with the bare words from the $message.

Is there a way to split the $message without removing the delimiters?
For example: "asd1 ,3" -> ["asd","1"," ",","3"]

Upvotes: 2

Views: 129

Answers (1)

Paul
Paul

Reputation: 141827

Yes, if you look at preg_split in the PHP manual, you'll see that can use the PREG_SPLIT_DELIM_CAPTURE flag with a capturing group:

preg_split("/([\s,<>123456789]+)/", $message, -1, PREG_SPLIT_DELIM_CAPTURE);

Upvotes: 4

Related Questions