Appearance
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. It natively handles apostrophes correctly, automatically capitalizing the letter that follows them. Here's an example:
Example of title case a string containing one or more last names while handling names with apostrophes in PHP
php
<โ?php
$name = "o'reilly";
$name = ucwords($name);
echo $name; // Outputs "O'Reilly"
<div class="alert alert-info flex not-prose">Watch a video course Learn object oriented PHP
</div>
Note that ucwords() works reliably for standard ASCII names. If you are working with multibyte strings or need to handle apostrophes in other positions, consider using mb_convert_case($name, MB_CASE_TITLE, 'UTF-8') instead.