hi, i have the following code: function sentence_case($string) { $sentences = preg_split('/([.?!\r\n]+)/', $string, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE); $new_string = ''; foreach ($sentences as $key => $sentence) { $new_string .= ($key & 1) == 0? ucfirst(strtolower(trim($sentence))) : $sentence.' '; } return trim($new_string); } PHP: the above works fine but the issue is with text on new lines. a space is inserted in front of the first word. is it possible to remove this space? thanks
function sentence_case($string) { $sentences = preg_split('/([.?!\r\n]+)/', $string, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE); $new_string = ''; foreach ($sentences as $key => $sentence) { $new_string .= ($key % 2 == 0)? ucfirst(strtolower(trim($sentence))) : $sentence; } return trim($new_string); } PHP:
hmmm, now that removes all spaces after punctuation... which again is not what's needed. i think it's very close close, any ideas? cheers
You are right, a little correction: function sentence_case($string) { $sentences = preg_split('/([.?!\s]+)/', $string, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE); $new_string = ''; foreach ($sentences as $key => $sentence) { $new_string .= ($key % 2 == 0)? ucfirst(strtolower(trim($sentence))) : $sentence; } return trim($new_string); } PHP: