0

How can I split a string by word? When I do $words = preg_split('/\b\w+\b/', $message); then the resulting array has the correct number of keys, but the array values are all blank?

2
Contributors
2
Replies
19
Views
4 Years
Discussion Span
Last Post by pritaeas
0

Nevermind ... I was able to accomplish what I need without using regex. Instead I used str_word_count()

$words = str_word_count($message, 1);

Edited by Dani

1

then the resulting array has the correct number of keys

To answer that for anybody else, preg_split uses the pattern to break the string. Since the \w+ is included it splits on the actual words, thus removing them.

Votes + Comments
Thanks!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.