Title Case a String Containing One or More Last Names While Handling Names with Apostrophes

Title case a string containing one or more last names while handling names with apostrophes

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))));

Python title() with apostrophes

If your titles do not contain several whitespace characters in a row (which would be collapsed), you can use string.capwords() instead:

>>> import string
>>> string.capwords("john's school")
"John's School"

EDIT: As Chris Morgan rightfully says below, you can alleviate the whitespace collapsing issue by specifying " " in the sep argument:

>>> string.capwords("john's    school", " ")
"John's School"

Capitalize last names including exceptions like mccall = McCall

$name = 'mccall';
$name = ucwords(strtolower(trim($name))) ;

if (strpos($name, 'Mc') === 0) {
$name = 'Mc' . ucwords(substr($name, 2, strlen($name)));
}
echo $name; // McCall

is it possible to use exemption in ucwords and strtolower?

You can try this -

$sample = 'UNITED STATES OF AMERICA';

function convert_str($str)
{
$except = array('of', 'in', 'is'); // Words you dont want to convert
$str= strtolower($str); // apply strtolower to all
return (in_array($str, $except)) ? $str : ucwords($str); // apply ucwords conditionally
}

$temp = explode(' ', $sample); // explode by space
$new_temp = array_map('convert_str', $temp); // apply the function to every element

echo implode(' ', $new_temp); // implode again

But it is for the text you provided. It might need to be changed accordingly.

Given upper case names transform to Proper Case, handling O'Hara , McDonald van der Sloot etc

Using regular expressions in a short provided list could be easy, but if you must handle hundreds or thousands of records it's very hard to be bullet proof.

I'd rather use something that can't affect someone else. How do you know if Mr. "MACDONALD" prefers "Macdonald"?

You're correcting someone else's error. If source cannot be corrected you could use something like this:

<?php

$provided_names = array(
"SMITH",
"O'HARA",
"MCDONALD",
"JONES",
"VAN DER SLOOT",
"MACDONALD"
);

$corrected_names = array(
"O'HARA" => "O'Hara",
"MCDONALD" => "McDonald",
"VAN DER SLOOT" => "van der Sloot"
);

$email_text = array();

foreach ($provided_names as $provided_name)
{
$provided_name = !array_key_exists($provided_name, $corrected_names)
? ucwords(strtolower($provided_name))
: $corrected_names[$provided_name];
$email_text[] = "{$provided_name}, your message text.";
}

print_r($email_text);

/* output:
Array
(
[0] => Smith, your message text.
[1] => O'Hara, your message text.
[2] => McDonald, your message text.
[3] => Jones, your message text.
[4] => van der Sloot, your message text.
[5] => Macdonald, your message text.
)
*/
?>

I hope it be useful.

Make string title case using ucfirst

ucfirst() only looks at the first character so you should convert to lowercase first.

Use this:

$produtto = 'APPLE';
echo ucfirst(strtolower($produtto));
//output: Apple


Related Topics



Leave a reply



Submit