ucwords

文字列の各単語の最初の文字を大文字にする

説明

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

文字が "a" (0x61) から "z" (0x7a) までのアルファベットの場合、 string の各単語の最初の文字を大文字にしたものを返します。

この関数における "単語" とは、 separators に含まれていない文字からなる文字列です。 separators のデフォルトはスペース、 フォームフィード、改行、キャリッジリターン、 水平タブ、垂直タブ です。

マルチバイト文字列に対して類似の変換を行うには、 mb_convert_caseMB_CASE_TITLE モードで使います。

パラメータ

string

入力文字列。

separators

オプションの separators で、単語の区切り文字を指定します。

戻り値

変更後の文字列を返します。

変更履歴

バージョン 説明
8.2.0 ケース変換は、setlocale で設定されたロケールに依存しなくなりました。 ASCII 文字のみが変換されます。

例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!
?>

例2 ucwords で、区切り文字を指定する例

<?php
$foo 
'hello|world!';
$bar ucwords($foo);             // Hello|world!

$baz ucwords($foo"|");        // Hello|World!
?>

例3 ucwords で、追加の区切り文字を指定する例

<?php
$foo 
"mike o'hara";
$bar ucwords($foo);                 // Mike O'hara

$baz ucwords($foo" \t\r\n\f\v'"); // Mike O'Hara
?>

注意

注意: この関数はバイナリデータに対応しています。

参考

  • strtoupper
  • strtolower
  • ucfirst
  • mb_convert_case