Open In App

How to convert first character of all the words uppercase using PHP ?

Last Updated : 18 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To convert the first character of all the words present in a string to uppercase, we just need to use one PHP function i.e. ucwords().

ucwords(string,delimiters)

This function takes 2 parameters. The first one is the string which is mandatory. The second parameter is the delimiter. 

Example 1:

PHP
<?php
  echo ucwords("welcome to geeks for geeks");
?>

Output
Welcome To Geeks For Geeks

 The second parameter(delimiter) is optional. If we don't mention it, then it will capitalize all the words after space. The echo keyword is used to print the output on the screen.

Example 2:

PHP
<?php
    // code
  echo ucwords("hey!let's get started","!")
?>

Output
Hey!Let's get started

As "!" is the delimiter, it will only capitalize the first letter of the word before and after "!".

Example 3:

PHP
<?php
    // code
  $str = 'php is fun learning';
  $str = ucwords($str);
  echo $str
?>

Output
Php Is Fun Learning

For storing a string in a variable we need to put $ sign in front of the variable.

Using a foreach Loop

Using a foreach loop, split the string into words using explode(). Iterate through each word with foreach, capitalize the first character using ucfirst(), then join the words back into a string with implode().

Example: In this example capitalizes the first letter of each word in the string "hello, world!" using explode(), ucfirst(), and implode(), resulting in "Hello, World!".

PHP
<?php
$string = "hello, world!";
$words = explode(' ', $string);
foreach ($words as &$word) {
    $word = ucfirst($word);
}
$capitalizedString = implode(' ', $words);
echo $capitalizedString;
?>

Output
Hello, World!

Using preg_replace_callback

In this method, preg_replace_callback is used to match the first character of each word and apply the strtoupper function to capitalize it. This approach gives us more control over the matching process and can be easily customized.

Example:

PHP
<?php
$string = "hello, world! let's code.";

$capitalizedString = preg_replace_callback('/\b\w/', function($matches) {
    return strtoupper($matches[0]);
}, $string);

echo $capitalizedString;
?>

Output
Hello, World! Let'S Code.

Approach: Using mb_convert_case()

The mb_convert_case() function in PHP is used to perform case conversions on multi-byte strings. It can be used to convert the first character of all the words present in a string to uppercase by utilizing the MB_CASE_TITLE mode.

Example:

PHP
<?php
$inputString = "hello, world! welcome to geeks for geeks.";

// Convert the first character of each word to uppercase
$outputString = mb_convert_case($inputString, MB_CASE_TITLE, "UTF-8");

echo "Original String: " . $inputString . "\n";
echo "Modified String: " . $outputString;
?>

Output
Original String: hello, world! welcome to geeks for geeks.
Modified String: Hello, World! Welcome To Geeks For Geeks.



Next Article

Similar Reads

  翻译: