note 41376 added to function.ucfirst
| From: | (ami@mistress.name) | Date: | Thu, 08 Apr 2004 00:34:49 +0000 |
| Subject: | note 41376 added to function.ucfirst | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-67790@lists.php.net to get a copy of this message | ||
In the event you sort of need multiple delimiters to apply the same action to, you can preg_replace
this "second delimiter" enveloping it with your actual delimiter.
A for instance, would be if you wanted to use something like Lee's FormatName function in an
input box designed for their full name as this script was only designed to check the last name as if
it were the entire string. The problem is that you still want support for double-barreled names and
you still want to be able to support the possibility that if the second part of the double-barreled
name starts with "mc", that it will still be formatted correctly.
This example does a preg_replace that surrounds the separator with your actual delimiter. This is
just a really quick alternative to writing some bigger fancier blah-blah function. If there's
a shorter, simpler way to do it, feel free to inform me. (Emphasis on shorter and simpler because
that was the whole point of this.) :D
Here's the example. I've removed Lee's comments as not to confuse them with my own.
<?php
function FormatName($name=NULL)
{
if (empty($name))
return false;
$name = strtolower($name);
$name = preg_replace("[\-]", " - ",$name); // Surround hyphens with our
delimiter so our strncmp is accurate
if (preg_match("/^[a-z]{2,}$/i",$name)) // Simple preg_match if statement
{
$names_array = explode(' ',$name); // Set the delimiter as a space.
for ($i = 0; $i < count($names_array); $i++)
{
if (strncmp($names_array[$i],'mc',2) == 0 ||
ereg('^[oO]\'[a-zA-Z]',$names_array[$i]))
{
$names_array[$i][2] = strtoupper($names_array[$i][2]);
}
$names_array[$i] = ucfirst($names_array[$i]);
}
$name = implode(' ',$names_array);
$name = preg_replace("[ \- ]", "-",$name); // Remove the extra instances
of our delimiter
return ucwords($name);
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.ucfirst.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41376
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41376&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41376&report=yes
Search -- http://master.php.net/manage/user-notes.php