PHP Snippets

Convert String To Title Case

Andrei Surdu Andrei Surdu ·

This function converts a string to title case. It first converts the entire string to lowercase using strtolower, then uses ucwords to capitalize the first letter of each word. This is useful for formatting text to make it more readable or aesthetically pleasing.

PHP Function:

function toTitleCase($str) { return ucwords(strtolower($str)); }

// Example
echo toTitleCase('hello, world!'); // 'Hello, World!'
echo toTitleCase('PHP IS GREAT'); // 'Php Is Great'

Comments

Share your thoughts and join the conversation

Loading comments...

Leave a Comment

Your email will not be published

Comments are moderated and will appear after approval

0/2000