01-21-2004, 10:53 AM
I have used the function str_word_count to split a text into words but this function will ignore colons so that a string like this
is split into "ProperName" and "Fred"
I would like the colon included so that the result is
"ProperName:" and "Fred"
What would be a good way of doing that?
01-21-2004, 11:36 AM
I think if you use the explode function the colons will remain (although i never tested it).
$string = 'ProperName: Fred';
$string_new = explode(" ", $string);
Hope this helps
01-21-2004, 04:06 PM
The problem with explode() is that you get empty elements in the result array if the string contains words seperated by more than one space. Also, words seperated by line-breaks, tabs etc. are not distinguished either.
You can perform a split along word boundaries more precisely with preg_split:
$result = preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY);
01-22-2004, 01:45 AM
Thanks I'll try them out and see if they do what I need. The strings I have do form sentences and paragraphs. I just want each word but to include the semicolon (and only the semicolon) if it's used.
01-23-2004, 06:08 AM
Thanks to both of you for you help. Mordred you case was much what I was after. I had to delve into the ugly world of reg expr. But
$psmethod = preg_split('/[\s,.]+/', $string, -1, PREG_SPLIT_NO_EMPTY);
gave me what I was after