note 86902 modified in function.ucfirst by danbrown
| From: | danbrown@php.net | Date: | Mon, 10 Nov 2008 04:02:41 +0000 |
| Subject: | note 86902 modified in function.ucfirst by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146337@lists.php.net to get a copy of this message | ||
This is what I use for converting strings to sentence case:
<?php
function sentence_case($string) {
$sentences = preg_split('/([.?!]+)/', $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);
}
print sentence_case('HMM. WOW! WHAT?');
// Outputs: "Hmm. Wow! What?"
?>
--was--
This is what I use for converting strings to sentence case:
function sentence_case($string) {
$sentences = preg_split('/([.?!]+)/', $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);
}
print sentence_case('HMM. WOW! WHAT?');
// Outputs: "Hmm. Wow! What?"
http://php.net/manual/en/function.ucfirst.php