PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




ucwords

Name

ucwords — Converts the first character of every word in a string to uppercase.

Synopsis

string ucwords(string);
string string:

Returns

String; FALSE on error

Description

ucwords() converts the first character of every word in a string to uppercase.

The function operates in a fairly simple manner. It loops through each character in a string. When it encounters a whitespace character, it attempts to convert the next character to uppercase. (In most cases, this function considers spaces, horizontal tabs, linefeeds, vertical tabs, formfeeds, and carriage returns to be whitespace - this may depend on the C compiler used.) It also attempts to convert the first character in the string to uppercase.

Note

This behavior means that ucwords() doesn't always perform as expected. Words led by non-whitespace characters such as "chicken-like" or (sometimes) are not handled correctly.

Don't expect this function to fix sentences that have odd capitalization. For example, ucwords() converts the string aLi bAbA to ALi BAbA, not Ali Baba(as you might hope).



Caution

This function is likely to give unexpected results with non-ASCII character sets. In cases like this, it's best to code your own replacement function. See the example.



The next example allows the developer to easily deal with any character set that has simple capitalization rules. The developer creates a simple array that maps a lowercase letter to its uppercase equivalent, and passes the array and the string to be transformed to the function.

Caution

This function changes the case on tags for HTML, XML, and so on.



Version

PHP 3.0.3+, PHP 4+

See also

To convert the first character of the first word in a string to uppercase:

ucfirst()

To change the case of a string:

strtolower()

strtoupper()



Example

Example 1244. Basic use of ucwords()

<?php
$string = <<<_JABBERWOCKY_
"and, has thou slain the jabberwock?
come to my arms, my beamish boy!
o frabjous day! callooh! callay!"
he chortled in his joy.
_JABBERWOCKY_;

// Compare the string before and after
// Note that some of the capitalization is broken
echo ucwords($string);
?>

Output:"and, Has Thou Slain The Jabberwock?
Come To My Arms, My Beamish Boy!
O Frabjous Day! Callooh! Callay!"
He Chortled In His Joy.

Example 1245. Sample replacement for ucwords()

<?php
function custom_ucwords($map, $string) {
   // A state variable that tracks if we are inside
   // or outside a block of word characters
   $inside_word = TRUE;

   for ($index = 0; $index < strlen($string); ++$index) {

      // If the current character is a key for the map array,
      // we know that the current character is a word character
      $is_chr = isset($map[$string[$index]]);

      /* If the last character was not a word character
       * but the current character is, convert the
       * current character to uppercase
      */
      if (! $inside_word && $is_chr) {
          $string[$index] = $map[$string[$index]];
      }

      // Track whether this character is a word or a non-word character
      // for the next iteration of the loop
      $inside_word = $is_chr;
   }

   return $string;
}

// A map for English characters
$map = array(
   'a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D',
   'e' => 'E', 'f' => 'F', 'g' => 'G', 'h' => 'H',
   'i' => 'I', 'j' => 'J', 'k' => 'K', 'l' => 'L',
   'm' => 'M', 'n' => 'N', 'o' => 'O', 'p' => 'P',
   'q' => 'Q', 'r' => 'R', 's' => 'S', 't' => 'T',
   'u' => 'U', 'v' => 'V', 'w' => 'W', 'x' => 'X',
   'y' => 'Y', 'z' => 'Z'
);

$string = <<<_JABBERWOCKY_
"and, has thou slain the jabberwock?<br />
come to my arms, my beamish boy!<br />
o frabjous day! callooh! callay!"<br />
he chortled in his joy.<br />
_JABBERWOCKY_;

echo custom_ucwords($map, $string);
?>



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.