PHP ucwords() Function

What is PHP ucwords() Function?

If you want to convert the first character of each word of an ASCII string to uppercase, use ucwords() function. More specifically, it only modifies the first byte of the each word of the string.

Syntax:

ucwords(string, separator)

Parameters:

The Function has 1 required parameter and 1 optional parameter-

string (Required): It specifies a string

separator (Optional): It specifies characters that define word boundaries. Here, a word is any sequence of characters separated by the characters in the “separator” parameter. You can create your own custom separators. Check example 2. By default, the function considers the following as boundaries-

  • An ordinary space  – “ ”
  • A tab – “\t”
  • A new line (line feed) – “\n”
  • A carriage return – “\r”
  • A form-feed – “\f”
  • A vertical tab – “\v”

Return Values:

The function returns a new converted string which has the first character of each word uppercased.

Examples:

Example 1:

<?php
$string = "learn web development from schools of web";
echo ucwords($string);
?>

Output:

Learn Web Development From Schools Of Web

Example 2:

<?php
$string = "learn|web|development|from|schools of|web";
echo ucwords($string, "|");
?>

Output:

Learn|Web|Development|From|Schools of|Web

Practical Usages of ucwords() Function:

  • Formatting names.
  • Formatting titles.
  • Formatting headlines.

Notes on ucwords() Function:

  • It only changes the first character. So, if the first character is a number (non-alphabetic character), the function doesn’t change anything though the second character might be a alphabetic character.
  • As it doesn’t lowercase the remaining characters other than the first one, to make a proper title case, the function is often used with the strtolower() function to lowercase the remaining characters. Check following example-
    <?php
    $string = "learn wEB Designing, wEB FROM SCHOOLs of web";
    echo ucwords(strtolower($string));
    ?>
    

    Output:

    Learn Web Designing, Web From Schools Of Web
  • The function works only on ASCII lowercase characters- a-z. It has no effect on the following characters-
    • Non ASCII characters or multi byte characters.
    • Numbers.
  • The function is binary-safe. This function considers its input as byte. It handles binary data (i.e. non-ASCII bytes, UTF-8) correctly. Check example below-
    <?php
    $string = "\x73chools of web";
    $convertedString = ucwords($string);
    echo "The original string is: " . $string . "<br />";
    echo "After running the function, it returns: " . $convertedString;
    ?>
    

    Output:

    The original string is: schools of web
    After running the function, it returns: Schools Of Web

    Explanation:

    \x73 is a hexadecimal which is equivalent to uppercase “s”. The ucwords() function converts the hexadecimal to its uppercase which is capital “S”.

  • As the function has no effect on multi byte characters, you can use mb_convert_case() function for multi byte characters.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP ucwords() Function

PHP’s ucwords() function is a built-in string function. Use this function to make first letters of every word uppercased of a string.

Reference:

https://www.php.net/manual/en/function.ucwords.php