超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<trimucwords>
Last updated: Fri, 22 Jun 2007

ucfirst

(PHP 3, PHP 4 , PHP 5)

ucfirst -- Make a string's first character uppercase

Description

string ucfirst ( string str)

Returns a string with the first character of str capitalized, if that character is alphabetic.

Note that 'alphabetic' is determined by the current locale. For instance, in the default "C" locale characters such as umlaut-a (ä) will not be converted.

例子 1. ucfirst() example

<?php
$foo
= 'hello world!';
$foo = ucfirst($foo);            // Hello world!

$bar = 'HELLO WORLD!';
$bar = ucfirst($bar);            // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
?>

See also strtolower(), strtoupper(), and ucwords().




add a note add a note User Contributed Notes
ucfirst
bkimble at ebaseweb dot com
09-Jun-2003 08:02
Here is a handy function that makes the first letter of everything in a sentence upercase. I used it to deal with titles of events posted on my website ... I've added exceptions for uppercase words and lowercase words so roman numeral "IV" doesn't get printed as "iv" and words like "a" and "the" and "of" stay lowercase.

function RemoveShouting($string)
{
$lower_exceptions = array(
       "to" => "1", "a" => "1", "the" => "1", "of" => "1"
);
                                   
$higher_exceptions = array(
       "I" => "1", "II" => "1", "III" => "1", "IV" => "1",
       "V" => "1", "VI" => "1", "VII" => "1", "VIII" => "1",
      "XI" => "1", "X" => "1"
);

$words = split(" ", $string);
$newwords = array();

foreach ($words as $word)
{
       if (!$higher_exceptions[$word])
               $word = strtolower($word);
       if (!$lower_exceptions[$word])
             $word = ucfirst($word);
        array_push($newwords, $word);

}
       
return join(" ", $newwords);  
}

<trimucwords>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22