I would like to get each word of this text but need to consider comma as a separate word, in PHP:
My input text:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
My wanted array:
array[0] => "Lorem"
array[1] => "ipsum"
array[2] => "dolor"
array[3] => "sit"
array[4] => "amet"
array[5] => ","
array[6] => "consectetuer"
array[7] => "adipiscing"
array[8] => "elit"
array[9] => "."
What I get with explode(" ", $text)
is:
array[0] => "Lorem"
array[1] => "ipsum"
array[2] => "dolor"
array[3] => "sit"
array[4] => "amet,"
array[5] => "consectetuer"
array[6] => "adipiscing"
array[7] => "elit."
You could replace the comma with a space+comma+space ',' -> ' , '
$newSentence = str_replace("," , " , " , $theSentence);
$arr = preg_split('/[\s]+/', $newSentence);