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

In PHP, you can use the ucwords() function to convert a string to title case. To handle last names with apostrophes, you can use the str_replace() function to replace the apostrophe with the appropriate HTML entity before passing the string to ucwords(). Here's an example:


$name = "o'reilly";
$name = str_replace("'", "&#39;", $name);
$name = ucwords($name);
// html_entity_decode() decodes the HTML entities in the string
$name = html_entity_decode($name);
echo $name; // Outputs "O'Reilly"

Watch a course Learn object oriented PHP

Note that this code will not handle all cases of names with apostrophes, as it only handles apostrophes at the beginning of the name. If you need to handle apostrophes in the middle or end of the name, you will need to use a more complex solution.