note 106498 added to function.ucfirst
| From: | rommel at rommelsantor dot com | Date: | Mon, 14 Nov 2011 18:00:10 +0000 |
| Subject: | note 106498 added to function.ucfirst | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184437@lists.php.net to get a copy of this message | ||
I was just noticing all of the lengthy implementations for achieving uppercase sentences (aka,
ucsentence or sentencecase). Do yourself a favor and avoid all the extensive code snippets. You can
achieve uppercase-sentencing with a single line:
<?php
function ucsentence($i_str, $i_lowercase = false) {
$i_lowercase && ($i_str = strtolower($i_str));
return preg_replace('/((?:^|[\.!?]"?\s+))([a-z])/e', '"$1" .
ucfirst("$2")', $i_str);
}
?>
Of course, there can be no complete automated solution because there may be some terms that
you'd prefer stay upper-cased (e.g., proper nouns, initials, abbreviations, etc.), but this
does a good job of the most basic requirement of making the first letter of a sentence uppercase.
Example:
<?php
$text = 'this is a test of the "Emergency Broadcast System!" this is only a test! or
is it...?';
//
// without lowercasing the string first
//
ucsentence($text);
//
// Output:
// This is a test of the "Emergency Broadcast System!" This is only a test! Or is it...?
//
//
// with lowercasing the string first
//
ucsentence($text, true);
//
// Output:
// This is a test of the "emergency broadcast system!" This is only a test! Or is it...?
//
?>
----
Server IP: 69.147.83.197
Probable Submitter: 98.189.22.30
----
Manual Page -- http://www.php.net/manual/en/function.ucfirst.php
Edit -- https://master.php.net/note/edit/106498
Del: integrated -- https://master.php.net/note/delete/106498/integrated
Del: useless -- https://master.php.net/note/delete/106498/useless
Del: bad code -- https://master.php.net/note/delete/106498/bad+code
Del: spam -- https://master.php.net/note/delete/106498/spam
Del: non-english -- https://master.php.net/note/delete/106498/non-english
Del: in docs -- https://master.php.net/note/delete/106498/in+docs
Del: other reasons-- https://master.php.net/note/delete/106498
Reject -- https://master.php.net/note/reject/106498
Search -- https://master.php.net/manage/user-notes.php