Skip to content

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 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.

Dual-run preview โ€” compare with live Symfony routes.