All, I\'m trying to insert a last name into a database. I\'d like the first letter to be capitalized for the name and if they have use two last names then capitalize the fir
use like this ucfirst(strtolower($var));
I don't believe there will be one good answer that covers all scenarios for you. The PHP.net forum for ucwords has a fair amount of discussions but none seem to have an answer for all. I would recommend that you standardize either using uppercase or leaving the user's input alone.
This will capitalize all word's first letters, and letters immediately after an apostrophe. It will make all other letters lowercase. It should work for you:
str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($last_name))));
None of these are UTF8 friendly, so here's one that works flawlessly (so far)
function titleCase($string, $delimiters = array(" ", "-", ".", "'", "O'", "Mc"), $exceptions = array("and", "to", "of", "das", "dos", "I", "II", "III", "IV", "V", "VI"))
{
/*
* Exceptions in lower case are words you don't want converted
* Exceptions all in upper case are any words you don't want converted to title case
* but should be converted to upper case, e.g.:
* king henry viii or king henry Viii should be King Henry VIII
*/
$string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
foreach ($delimiters as $dlnr => $delimiter) {
$words = explode($delimiter, $string);
$newwords = array();
foreach ($words as $wordnr => $word) {
if (in_array(mb_strtoupper($word, "UTF-8"), $exceptions)) {
// check exceptions list for any words that should be in upper case
$word = mb_strtoupper($word, "UTF-8");
} elseif (in_array(mb_strtolower($word, "UTF-8"), $exceptions)) {
// check exceptions list for any words that should be in upper case
$word = mb_strtolower($word, "UTF-8");
} elseif (!in_array($word, $exceptions)) {
// convert to uppercase (non-utf8 only)
$word = ucfirst($word);
}
array_push($newwords, $word);
}
$string = join($delimiter, $newwords);
}//foreach
return $string;
}
Usage:
$s = 'SÃO JOÃO DOS SANTOS';
$v = titleCase($s); // 'São João dos Santos'
This is a little more simple and more direct answer to the main question. the function below mimics the PHP approachs. Just in case if PHP extend this with their namespaces in the future, a test is first checked. Im using this water proof for any languages in my wordpress installs.
$str = mb_ucfirst($str, 'UTF-8', true);
This make first letter uppercase and all other lowercase as the Q was. If the third arg is set to false (default), the rest of the string is not manipulated.
// Extends PHP
if (!function_exists('mb_ucfirst')) {
function mb_ucfirst($str, $encoding = "UTF-8", $lower_str_end = false) {
$first_letter = mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding);
$str_end = "";
if ($lower_str_end) {
$str_end = mb_strtolower(mb_substr($str, 1, mb_strlen($str, $encoding), $encoding), $encoding);
} else {
$str_end = mb_substr($str, 1, mb_strlen($str, $encoding), $encoding);
}
$str = $first_letter . $str_end;
return $str;
}
}
/ Lundman
You can use strtolower
and ucwords
functions in PHP.
First: lower all inputted text using strtolower('inputtedtext') then capitalise all text using
ucwords('strtolower')`.
Sample :
$text = 'tHis iS sOme tEXt'; <br>
$lower = strtolower($text);   //this will lower all letter from the text <br>
$upper = ucwords($lower);   //this will uppercase all first letter from the text <br>
echo $upper;
Result = This Is Some Text
You can use one line code for this with ucwords(strtolower($text));