Hello Friends Today, through this tutorial, I will tell you How do you convert a string to title case in PHP With Example? In PHP, you can convert a string to title case using the `ucwords()` function. This function capitalizes the first letter of each word in a string. Here’s how you can use it:
<?php $string = "hello world"; $titleCaseString = ucwords($string); echo $titleCaseString; // Outputs: Hello World ?>
This function takes an optional second parameter, `$delimiters`, which specifies a string containing the characters to be treated as word separators. If provided, the function will capitalize the first letter after each of these delimiters. If omitted, it defaults to `” \t\r\n\f\v”`, which includes space, tab, newline, carriage return, form feed, and vertical tab.
Example using custom delimiters:
<?php $string = "hello-world"; $titleCaseString = ucwords($string, "-"); echo $titleCaseString; // Outputs: Hello-World ?>
This will capitalize the first letter after each hyphen (-) in the string.