Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

ucwords

(PHP 3 >= 3.0.3, PHP 4, PHP 5)

ucwords --  Преобразует в верхний регистр первый символ каждого слова в строке

Описание

string ucwords ( string str )

Возвращает строку string, в которой первый символ каждого слова переведен в верхний регистр, если этот символ буквенный.

Эта функция считает словами последовательности символов, разделенных пробелом, переводом строки, возвратом каретки, горизонтальной или вертикальной табуляцией.

Пример 1. Пример использования ucwords()

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

$bar = 'HELLO WORLD!';
$bar = ucwords($bar);             // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!
?>

Замечание: Эта функция безопасна для обработки данных в двоичной форме.

См. также описание функций strtoupper(), strtolower() и ucfirst().



vfprintf> <ucfirst
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
ucwords
kendsnyder at gmail dot com
30-Jul-2007 01:02
Here is a function to capitalize a last name, accounting for hyphens, apostrophes, "Mc" and "Mac":

<?php
function CapitalizeLastName($name) {
   
$name = strtolower($name);
   
$name = join("'", array_map('ucwords', explode("'", $name)));
   
$name = join("-", array_map('ucwords', explode("-", $name)));
   
$name = join("Mac", array_map('ucwords', explode("Mac", $name)));
   
$name = join("Mc", array_map('ucwords', explode("Mc", $name)));
    return
$name;
}
?>

I speed tested it against functions that used preg_replace() with an "e" modifier, preg_replace_callback(), and a character-by-character parsing.  Unexpectedly, this function using join(), array_map() and explode() was fastest.
emailfire at gmail dot com
23-May-2007 04:27
To use ucwords with an exception:

<?php
function ucwordss($str, $exceptions) {
$out = "";
foreach (
explode(" ", $str) as $word) {
$out .= (!in_array($word, $exceptions)) ? strtoupper($word{0}) . substr($word, 1) . " " : $word . " ";
}
return
rtrim($out);
}
?>

For example:

<?php
$string
= "my cat is going to the vet";
$ignore = array("is", "to", "the");
echo
ucwordss($string, $ignore);
// My Cat is Going to the Vet
?>
Q1712 at online dot ms
05-May-2007 12:49
ucwords() only excepts whitespace in front of a word, although some chars like '"' or '(' normally have no space between them and the following word:
<?php
$title
= 'ELVIS "THE KING" PRESLEY - (LET ME BE YOUR) TEDDY BEAR';
echo
ucwords(strtolower($title));
?>
prints: Elvis "the King" Presley - (let Me Be Your) Teddy Bear

To avoid this i use a small function adding and deleting blanks behind these chars, and using ucwords() in between:

<?php
function my_ucwords($string)
  {
   
$noletters='"([/'; //add more if u need to
   
for($i=0; $i<strlen($noletters); $i++)
     
$string = str_replace($noletters[$i], $noletters[$i].' ', $string);
   
$string=ucwords($string);
    for(
$i=0; $i<strlen($noletters); $i++)
     
$string = str_replace($noletters[$i].' ', $noletters[$i], $string);
    return
$string;
  }

$title = 'ELVIS "THE KING" PRESLEY - (LET ME BE YOUR) TEDDY BEAR';
echo
my_ucwords(strtolower($title));
?>

prints: Elvis "The King" Presley - (Let Me Be Your) Teddy Bear
chris at cmbuckley dot co dot uk
13-Feb-2007 09:16
To get some sort of title case with lower-case articles, prepositions etc., try something like this (removing the carriage returns in the regular expression):

<?php
   
function lower_articles($str) {
        return
preg_replace(
           
"/(?<=(?<!:|’s)\W)
            (A|An|And|At|For|In|Of|On|Or|The|To|With)
            (?=\W)/e"
,
           
'strtolower("$1")',
           
$str
       
);
    }
?>

I added the lookbehind (?<!:|’s) because I use this for film titles, where words following those terms should be capitalised (e.g. Lemony Snicket’s A Series of Unfortunate Events, American Pie Presents: The Naked Mile).
barnaby ritchley at exeye dot co dot uk
03-Jan-2007 08:53
A very easy way to convert to title case:

function titleCase($string)
     {
     return ucwords(strtolower($string));
     }

$myString = "SOME TEXT";

echo titleCase($myString);

//will print, "My Text"
max at phoenixweb dot it
15-Sep-2006 07:35
I have rewritten a UCSMART function adding a feature to translate special ASCII char (windows occidental ascii charset):

You can edit/add/delete char by use the first two string (be carefull to preserve the order of the string).

Enjoy!

<?
$ASCII_SPC_MIN = "
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571