ucwords
CONVERTS the first character of eache word to UPPERCASE in a STRING.
This function returns a string with the first character of each word converted to UPPERCASE if that character is alphabetic.
May experience conversion issues even with LOCALE information.
<?php
str ucwords ( str $str [, str $delimiters ] )
where,
$str = The input STRING
$delimiters = The word separator characters
?>
$str
The input STRING.
$delimiters
The word separator characters of words in the STRING.
EXERCISE
<?php
$str0010 = 'quandoque bonus dormitat Homerus';
$str0011 = ucwords ($str0010);
echo 'Original STRING:<br>' . $str0010 .
'<br><br>After ucwords:<br>' . $str0011 . '<br>';
?>
RESULT
Original STRING
quandoque bonus dormitat Homerus
After ucwords
Quandoque Bonus Dormitat Homerus
EXERCISE
<?php
$str0020 = 'à l’impossible nul n’est tenu';
$str0021 = ucwords ($str0020);
echo 'Original STRING:<br>' . $str0020 .
'<br><br>After ucwords:<br>' . $str0021 . '<br>';
?>
EXERCISE
<?php
$str0030 = 'Ça |coûte les *yeux de la |tête';
$str0031 = ucwords ($str0030, '|*');
echo 'Original STRING:<br>' . $str0030 .
'<br><br>After ucwords:<br>' . $str0031 . '<br>';
?>
EXERCISE
<?php
echo "Testing ucwords() : usage variations.<br><br>";
$v04a = 'testing-dashed-words';
$v04b = 'test(braced)words';
$v04c = 'testing empty delimiters';
$v04d = 'testing ranges';
echo "Original STRING:<br>'$v04a'<br>";
echo "After ucwords('$v04a', '-'):<br>";
echo ucwords($v04a, '-');
echo '<br><br>';
echo "Original STRING:<br>'$v04b'<br>";
echo "After ucwords('$v04b', '()'):<br>";
echo ucwords($v04b, '()');
echo '<br><br>';
echo "Original STRING:<br>'$v04c'<br>";
echo "After ucwords('$v04c', ''):<br>";
echo ucwords($v04c, '');
echo '<br><br>';
echo "Original STRING:<br>'$v04d'<br>";
echo "After ucwords('$v04d', 'a..e'):<br>";
echo ucwords($v04d, 'a..e');
?>
EXERCISE
<?php
/*
* test ucwords() with different
* string prepared using double quote
*/
echo "Testing ucwords() : usage variations.<br>";
// different strings containing
// regular chars and special chars
$str_array = array(
// multiple spaces
"testing ucwords",
"t e s t i n g u c w o r d s ",
// brackets in sentence
"testing function(ucwords)",
"(testing ( function (ucwords) )a )test",
"(t)",
" ( t )t",
// using quote chars in sentence
"\"testing\",ucwords,\"test\"",
"\"t\"\"t\",test, t",
"\'t \'t\',test",
"Jack's pen",
"P't'y 't it's ",
// using other white spaces
"\ttesting\ttesting\tucwords",
"\\ttesting\\ttesting\tucwords",
"testing\rucwords testing ucwords",
"testing\\rucwords testing ucwords",
"testing\fucwords \f testing \nucwords",
"testing\\fucwords \\f testing \nucwords",
"\ntesting\nucwords\n testing \n ucwords",
"\\ntesting\\nucwords\\n testing \\n ucwords",
"using\vvertical\vtab",
"using\\vvertical\\vtab",
//using special chars in sentence
"t@@#$% %test ^test &test *test +test -test",
"!test ~test `test` =test= @[email protected]",
"/test/r\test\\ucwords\t\y\y\\u\3 \yy\ /uu/",
//only special chars
"!@#$%^&*()_+=-`~"
);
// loop through the $str_array array
// to test ucwords on each element
$iteration = 1;
for($index = 0; $index < count($str_array); $index++) {
echo "<br><br>Iteration: $iteration<br>";
var_dump( ucwords($str_array[$index]) );
$iteration++;
}
?>
EXERCISE
<?php
/*
* test ucwords() with different
* string prepared using single quote
*/
echo "Testing ucwords() : usage variations.<br>";
// different strings containing
// regular chars and special chars
$str_array = array(
// multiple spaces
'testing ucwords',
't e s t i n g u c w o r d s ',
// brackets in sentence
'testing function(ucwords)',
'(testing ( function (ucwords) )a )test',
'(t)',
' ( t )t',
// using quote chars in sentence
'"testing",ucword,"test"',
'"t""t",test, t',
'\'t \'t\',test',
// using other white spaces
'\ttesting\ttesting\tucwords',
'testing\rucwords testing ucwords',
'testing\fucwords \f testing \nucwords',
'\ntesting\nucwords\n testing \n ucwords',
'using\vvertical\vtab',
//using special chars in sentence
't@@#$% %test ^test &test *test +test -test',
'!test ~test `test` =test= @[email protected]',
'/test/r\test\ucwords\t\y\y\u\3 \yy\ /uu/',
//only special chars
'!@#$%^&*()_+=-`~'
);
// loop through the $str_array array
// to test ucwords on each element
$iteration = 1;
for($index = 0; $index < count($str_array); $index++) {
echo "<br><br>Iteration: $iteration<br>";
var_dump( ucwords($str_array[$index]) );
$iteration++;
}
?>
EXERCISE
<?php
/*
* test ucwords() with different
* string prepared using heredoc
*/
echo "Testing ucwords() : usage variations.<br>";
// Null here doc string
$null_string = <<<EOT
EOT;
// Heredoc string with blank line
$blank_line = <<<EOT
EOT;
// here doc with multiline string
$multiline_string = <<<EOT
testing ucword() with
multiline string using
heredoc
EOT;
// here doc with different whitespaces
$diff_whitespaces = <<<EOT
testing\rucword(str)\twith
multiline string\t\tusing
heredoc\nstring.with\vdifferent\fwhite\vspaces
EOT;
// here doc with numeric values
$numeric_string = <<<EOT
12sting 123string 4567
string\t123string\r12 test\n5test
EOT;
// heredoc with quote chars & slash
$quote_char_string = <<<EOT
it's bright,but i cann't see it.
"things in double quote"
'things in single quote'
this\line is /with\slashs
EOT;
$heredoc_strings = array(
$null_string,
$blank_line,
$multiline_string,
$diff_whitespaces,
$numeric_string,
$quote_char_string
);
// loop through $heredoc_strings element
// and check the working on ucwords()
$count = 1;
for($index =0; $index < count($heredoc_strings); $index ++) {
echo "<br><br>Iteration: $count<br>";
var_dump( ucwords($heredoc_strings[$index]) );
$count ++;
}
?>
EXERCISE
<?php
echo "Testing ucwords() : basic functionality.<br>";
// lines with different whitespace character
$str_array = array(
"testing ucwords",
'testing ucwords',
'testing\tucwords',
"testing\tucwords",
"testing\nucwords",
'testing\nucwords',
"testing\vucwords",
'testing\vucwords',
"testing",
'testing',
' testing',
" testing",
"testing ucwords",
'testing ucwords',
'testing\rucwords',
"testing\rucwords",
'testing\fucwords',
"testing\fucwords"
);
// loop through the $strings array
// to test ucwords on each element
$iteration = 1;
for($index = 0; $index < count($str_array); $index++) {
echo "<br><br>Iteration: $iteration<br>";
var_dump( ucwords($str_array[$index]) );
$iteration++;
}
?>