Wednesday, 24 March 2021

Php: ucwords: Uppercase the first character of each word in a string

Signature

ucwords ( string $string , string $separators = " \t\r\n\f\v" ) : string

 

Description

Returns a string with the first character of each word in string capitalized, if that character is alphabetic. This will not update the actual string.

 

$separators argument is used to define what a word is. For example, I have following string.

$msg3 = "Hello|krishna,|how|are|you?";

 

In the above string, words are separated by | symbol, to make every first character in each word to uppercase, I can use separators argument.

$msg4 = ucwords($msg3, '|');

 

Find the below working application.

 

ucwords_demo.php

#!/usr/bin/php

<?php

    $msg1 = "Hello krishna, how are you?";
    $msg2 = ucwords($msg1);

    echo "msg1 : $msg1\n";
    echo "msg2 : $msg2\n";

    $msg3 = "Hello|krishna,|how|are|you?";
    $msg4 = ucwords($msg3, '|');

    echo "msg3 : $msg3\n";
    echo "msg4 : $msg4\n";
?>

 

Output

$./ucwords_demo.php 

msg1 : Hello krishna, how are you?
msg2 : Hello Krishna, How Are You?
msg3 : Hello|krishna,|how|are|you?
msg4 : Hello|Krishna,|How|Are|You?

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment