Регистр первой буквы на UTF8
Как правильно и коротко привести к верхнему регистру первую букву в русском UTF-8 тексте в PHP?
Отличается от ответа he11d0g тем, что в mb_substr передается длина строки (при передаче null не работает с кодировкой cp1251 на php5.3)
Я пользуюсь ucfirst. Товарищ ROOT дал ссылку на документацию.
Так же добавлю — примитивный способ)))
Дизайн сайта / логотип © 2023 Stack Exchange Inc; пользовательские материалы лицензированы в соответствии с CC BY-SA . rev 2023.3.13.43308
Нажимая «Принять все файлы cookie» вы соглашаетесь, что Stack Exchange может хранить файлы cookie на вашем устройстве и раскрывать информацию в соответствии с нашей Политикой в отношении файлов cookie.
ucfirst
Возвращает строку string , в которой первый символ переведён в верхний регистр, если этот символ является символом ASCII в диапазоне от "a" (0x61) до "z" (0x7a).
Список параметров
Возвращаемые значения
Возвращает результирующую строку.
Список изменений
| Версия | Описание |
|---|---|
| 8.2.0 | Преобразование регистра больше не зависит от локали, установленной с помощью функции setlocale() . Будут преобразованы только символы ASCII. |
Примеры
Пример #1 Пример использования ucfirst()
<?php
$foo = ‘hello world!’ ;
$foo = ucfirst ( $foo ); // Hello world!
$bar = ‘HELLO WORLD!’ ;
$bar = ucfirst ( $bar ); // HELLO WORLD!
$bar = ucfirst ( strtolower ( $bar )); // Hello world!
?>
Смотрите также
- lcfirst() — Преобразует первый символ строки в нижний регистр
- strtolower() — Преобразует строку в нижний регистр
- strtoupper() — Преобразует строку в верхний регистр
- ucwords() — Преобразует в верхний регистр первый символ каждого слова в строке
- mb_convert_case() — Производит смену регистра символов в строке
User Contributed Notes 35 notes
Simple multi-bytes ucfirst():
<?php
function my_mb_ucfirst ( $str ) <
$fc = mb_strtoupper ( mb_substr ( $str , 0 , 1 ));
return $fc . mb_substr ( $str , 1 );
>
?>
A proper Turkish solution;
<?php
function ucfirst_turkish ( $str ) <
$tmp = preg_split ( «//u» , $str , 2 , PREG_SPLIT_NO_EMPTY );
return mb_convert_case (
str_replace ( «i» , «İ» , $tmp [ 0 ]), MB_CASE_TITLE , «UTF-8» ).
$tmp [ 1 ];
>
$str = «iyilik güzelLİK» ;
echo ucfirst ( $str ) . «\n» ; // Iyilik güzelLİK
echo ucfirst_turkish ( $str ); // İyilik güzelLİK
?>
I believe that mb_ucfirst will be soon added in PHP, but for now this could be useful
<?php
if (! function_exists ( ‘mb_ucfirst’ ) && function_exists ( ‘mb_substr’ )) <
function mb_ucfirst ( $string ) <
$string = mb_strtoupper ( mb_substr ( $string , 0 , 1 )) . mb_substr ( $string , 1 );
return $string ;
>
>
?>
it also check is mb support enabled or not
This is what I use for converting strings to sentence case:
<?php
function sentence_case ( $string ) <
$sentences = preg_split ( ‘/([. ]+)/’ , $string , — 1 , PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
$new_string = » ;
foreach ( $sentences as $key => $sentence ) <
$new_string .= ( $key & 1 ) == 0 ?
ucfirst ( strtolower ( trim ( $sentence ))) :
$sentence . ‘ ‘ ;
>
return trim ( $new_string );
>
print sentence_case ( ‘HMM. WOW! WHAT?’ );
Implementation of multi-bytes ucfirst for «multiword»-strings (module mbstring is required):
public static function ucfirst ( $str )
<
$str = mb_strtolower ( $str );
$words = preg_split ( ‘/\b/u’ , $str , — 1 , PREG_SPLIT_NO_EMPTY );
foreach ( $words as $word ) <
$ucword = mb_strtoupper ( mb_substr ( $word , 0 , 1 )) . mb_substr ( $word , 1 );
$str = str_replace ( $word , $ucword , $str );
>
return $str ;
>
plemieux’ function did not work for me without passing the encoding to every single mb function (despite ini_set(‘default_charset’, ‘utf-8’) at the top of the script). This is the example that works in my application (PHP 4.3):
<?php
function my_mb_ucfirst ( $str , $e = ‘utf-8’ ) <
$fc = mb_strtoupper ( mb_substr ( $str , 0 , 1 , $e ), $e );
return $fc . mb_substr ( $str , 1 , mb_strlen ( $str , $e ), $e );
>
?>
Here’s a function to capitalize segments of a name, and put the rest into lower case. You can pass the characters you want to use as delimiters.
i.e. <?php echo nameize ( «john o’grady-smith» ); ?>
returns John O’Grady-Smith
function nameize ( $str , $a_char = array( «‘» , «-» , » » )) <
//$str contains the complete raw name string
//$a_char is an array containing the characters we use as separators for capitalization. If you don’t pass anything, there are three in there as default.
$string = strtolower ( $str );
foreach ( $a_char as $temp ) <
$pos = strpos ( $string , $temp );
if ( $pos ) <
//we are in the loop because we found one of the special characters in the array, so lets split it up into chunks and capitalize each one.
$mend = » ;
$a_split = explode ( $temp , $string );
foreach ( $a_split as $temp2 ) <
//capitalize each portion of the string which was separated at a special character
$mend .= ucfirst ( $temp2 ). $temp ;
>
$string = substr ( $mend , 0 ,- 1 );
>
>
return ucfirst ( $string );
>
I made a small change. Now it takes care of points in numbers
function ucsentence ($string) <
$string = explode (‘.’, $string);
$count = count ($string);
for ($i = 0; $i < $count; $i++) <
$string[$i] = ucfirst (trim ($string[$i]));
if ($i > 0) <
if ((ord($string[$i]<0>)<48) || (ord($string[$i]<0>)>57)) <
$string[$i] = ‘ ‘ . $string[$i];
>
>
>
$string = implode (‘.’, $string);
return $string;
>
My version, converst first letter of the first word in the string to uppercase
public function mb_ucfirst($str) <
$aParts = explode(» «,$str);
$firstWord = mb_convert_case($aParts[0],MB_CASE_TITLE,»UTF-8″);
unset($aParts[0]);
return $firstWord.» «.implode(» «,$aParts);
>
For some reason this worked for me.
Mac OS 10.5.1
PHP 5.2.6
<?php
/**
* ucfirst UTF-8 aware function
*
* @param string $string
* @return string
* @see http://ca.php.net/ucfirst
*/
function my_ucfirst ( $string , $e = ‘utf-8’ ) <
if ( function_exists ( ‘mb_strtoupper’ ) && function_exists ( ‘mb_substr’ ) && !empty( $string )) <
$string = mb_strtolower ( $string , $e );
$upper = mb_strtoupper ( $string , $e );
preg_match ( ‘#(.)#us’ , $upper , $matches );
$string = $matches [ 1 ] . mb_substr ( $string , 1 , mb_strlen ( $string , $e ), $e );
> else <
$string = ucfirst ( $string );
>
return $string ;
>
?>
Svetoslav Marinov
http://slavi.biz
Simple function for use ucfirst with utf-8 encoded cyrylic text
<?php
public function capitalize_first ( $str ) <
$line = iconv ( «UTF-8» , «Windows-1251» , $str ); // convert to windows-1251
$line = ucfirst ( $line );
$line = iconv ( «Windows-1251» , «UTF-8» , $line ); // convert back to utf-8
Improved method of capitalizing first characters of sentences.
The first two manipulations (double spaces & all caps) are optional so can be removed without harm.
<?php
// return string with first letters of sentences capitalized
function ucsentence ( $str ) <
if ( $str ) < // input
$str = preg_replace ( ‘/’ . chr ( 32 ). chr ( 32 ). ‘+/’ , chr ( 32 ), $str ); // recursively replaces all double spaces with a space
if (( $x = substr ( $str , 0 , 10 )) && ( $x == strtoupper ( $x ))) $str = strtolower ( $str ); // sample of first 10 chars is ALLCAPS so convert $str to lowercase; if always done then any proper capitals would be lost
$na = array( ‘. ‘ , ‘! ‘ , ‘? ‘ ); // punctuation needles
foreach ( $na as $n ) < // each punctuation needle
if ( strpos ( $str , $n ) !== false ) < // punctuation needle found
$sa = explode ( $n , $str ); // split
foreach ( $sa as $s ) $ca [] = ucfirst ( $s ); // capitalize
$str = implode ( $n , $ca ); // replace $str with rebuilt version
unset( $ca ); // clear for next loop
>
>
return ucfirst ( trim ( $str )); // capitalize first letter in case no punctuation needles found
>
>
?>
«heLLo EarthLing!» >> «HeLLo EarthLing!»
«I’M MOSTLY. caps! » >> «I’m mostly. Caps!»
«ALLCAPS» >> «Allcaps»
«i haVe neST.ed punct,u.ation! sp A c es. and CAPs.. » >> «I haVe neST.ed punct,u.ation! Sp A c es. And CAPs..»
if you want to ucfirst for utf8 try this one:
<?php
function ucfirst_utf8 ( $stri ) <
if( $stri < 0 >>= «\xc3» )
return (( $stri < 1 >>= «\xa0» )?
( $stri < 0 >. chr ( ord ( $stri < 1 >)- 32 )):
( $stri < 0 >. $stri < 1 >)). substr ( $stri , 2 );
else return ucfirst ( $stri );
>
?>
It is quick, not language (but utf8) dependend and does not use any mb-functions such as mb_ucfirst.
Here is the fixed function for Turkish alphabet..
function uc_first ( $str ) <
$str [ 0 ] = strtr ( $str ,
«abcdefgh?ijklmnopqrstuvwxyz» .
«\x9C\x9A\xE0\xE1\xE2\xE3» .
«\xE4\xE5\xE6\xE7\xE8\xE9» .
«\xEA\xEB\xEC\xED\xEE\xEF» .
«\xF0\xF1\xF2\xF3\xF4\xF5» .
«\xF6\xF8\xF9\xFA\xFB\xFC» .
«\xFE\xFF» ,
«ABCDEFGHI?JKLMNOPQRSTUVWXYZ» .
«\x8C\x8A\xC0\xC1\xC2\xC3\xC4» .
«\xC5\xC6\xC7\xC8\xC9\xCA\xCB» .
«\xCC\xCD\xCE\xCF\xD0\xD1\xD2» .
«\xD3\xD4\xD5\xD6\xD8\xD9\xDA» .
«\xDB\xDC\xDE\x9F» );
return $str ;
>
for anyone wanting to ucfirst each word in a sentence this works for me:
<?php
function ucfirst_sentence ( $str )
<
return preg_replace ( ‘/\b(\w)/e’ , ‘strtoupper(«$1»)’ , $str );
>
?>
For lithuanian text with utf-8 encoding I use two functions (thanks [mattalexxpub at gmail dot com] and Svetoslav Marinov)
<?php
function my_ucfirst ( $string , $e = ‘utf-8’ ) <
if ( function_exists ( ‘mb_strtoupper’ ) && function_exists ( ‘mb_substr’ ) && !empty( $string )) <
$string = mb_strtolower ( $string , $e );
$upper = mb_strtoupper ( $string , $e );
preg_match ( ‘#(.)#us’ , $upper , $matches );
$string = $matches [ 1 ] . mb_substr ( $string , 1 , mb_strlen ( $string , $e ), $e );
>
else <
$string = ucfirst ( $string );
>
return $string ;
>
function sentence_case ( $string ) <
$sentences = preg_split ( ‘/([. ]+)/’ , $string , — 1 , PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
$new_string = » ;
foreach ( $sentences as $key => $sentence ) <
$new_string .= ( $key & 1 ) == 0 ?
my_ucfirst ( strtolower ( trim ( $sentence ))) :
$sentence . ‘ ‘ ;
>
return trim ( $new_string );
>
?>
<?php
mb_internal_encoding ( «UTF-8» );
mb_regex_encoding ( «UTF-8» );
function tr_ilkbuyuk ( $text )
<
$text = str_replace ( «I» , «ı» , $text );
$text = mb_strtolower ( $text , ‘UTF-8’ );
if( $text [ 0 ] == «i» )
$tr_text = «İ» . substr ( $text , 1 );
else
$tr_text = mb_convert_case ( $text , MB_CASE_TITLE , «UTF-8» );
return trim ( $tr_text );
>
function tr_ucwords ( $text )
<
$p = explode ( » » , $text );
if( is_array ( $p ))
<
$tr_text = «» ;
foreach( $p AS $item )
$tr_text .= » » . tr_ilkbuyuk ( $item );
return trim ( $tr_text );
>
else
return tr_ilkbuyuk ( $text );
>
echo tr_ucwords ( $deger );
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»
);
$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);
>
One thing I would do to make this more unviersally work would be to add strtolower() around your $sentence. Doing this will allow you to convert an all caps text block as well as an all lowercase text block.
function sentence_cap ( $impexp , $sentence_split ) <
$textbad = explode ( $impexp , $sentence_split );
$newtext = array();
foreach ( $textbad as $sentence ) <
$sentencegood = ucfirst ( strtolower ( $sentence ));
$newtext [] = $sentencegood ;
>
$textgood = implode ( $impexp , $newtext );
return $textgood ;
>
$text = «this is a sentence. this is another sentence! this is the fourth sentence? no, this is the fourth sentence.» ;
$text = sentence_cap ( «. » , $text );
$text = sentence_cap ( «! » , $text );
$text = sentence_cap ( «? » , $text );
echo $text ; // This is a sentence. This is another sentence! This is the fourth sentence? No, this is the fourth sentence.
Using this function for Turkish language is won’t work because of multi-byte characters. But you can use some tricks:
<?php
function ucfirst_tr ( $str ) <
$trMap = [ ‘Ğ’ => ‘ğ’ , ‘Ü’ => ‘ü’ , ‘Ş’ => ‘ş’ , ‘İ’ => ‘i’ , ‘Ö’ => ‘ö’ , ‘Ç’ => ‘ç’ , ‘I’ => ‘ı’ ];
$str = mb_strtolower ( strtr ( $str , $trMap ));
$first = mb_substr ( $str , 0 , 1 );
$first = strtr ( $first , array_flip ( $trMap ));
$first = mb_strtoupper ( $first );
return $first . mb_substr ( $str , 1 );
>
?>
Format the input string:
function ucsentences ( $string ) <
$parts = preg_split ( ‘/([^\.\!\?;]+[\.\!\?;»]+)/’ , strtolower ( $string ), (- 1 ), PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
$r = » ;
foreach( $parts as $key => $sentence ) <
$r .= ucfirst ( trim ( $sentence )) . ‘ ‘ ;
>
$r = preg_replace ( ‘/\bi\b/’ , ‘I’ , $r );
$r = preg_replace_callback ( ‘/(«[a-z])/’ , function( $m )< return strtoupper ( $m [ 0 ]);>, $r );
return rtrim ( $r );
>
$str = ‘i\’m not sure. if this is good enough, but i thought: «hey, who know\’s. maybe i am right.»‘ ;
?>
Outputs:
I’m not sure. If this is good enough, but I thought: «Hey, who know’s. Maybe I am right.»
This is a simple code to get all the ‘bad words’, stored in a database, out of the text. You could use str_ireplace but since that’s installed on PHP5 only, this works as well. It strtolowers the text first then places capitals with ucfirst() where it thinks a capital should be placed, at a new sentence. The previous sentence is ended by ‘. ‘ then.
<?php
function filter ( $text ) <
$filters = mysql_query ( «SELECT word,result FROM filter» );
while( $filter = mysql_fetch_array ( $filters )) <
$text = str_replace ( $filter [ word ], $filter [ result ], strtolower ( $text ));
$parts = explode ( «. » , $text );
for( $i = 0 ; $i < count ( $parts ); $i ++) <
$parts [ $i ]= ucfirst ( $parts [ $i ]);
>
$text = implode ( «. » , $parts );
>
return $text ;
>
?>
Ah, the last code were spoiled, here is the fixed one:
function uc_first ( $str ) <
$str [ 0 ] = strtr ( $str ,
«abcdefghijklmnopqrstuvwxyz» .
«\x9C\x9A\xE0\xE1\xE2\xE3» .
«\xE4\xE5\xE6\xE7\xE8\xE9» .
«\xEA\xEB\xEC\xED\xEE\xEF» .
«\xF0\xF1\xF2\xF3\xF4\xF5» .
«\xF6\xF8\xF9\xFA\xFB\xFC» .
«\xFD\xFE\xFF» ,
«ABCDEFGHIJKLMNOPQRSTUVWXYZ» .
«\x8C\x8A\xC0\xC1\xC2\xC3\xC4» .
«\xC5\xC6\xC7\xC8\xC9\xCA\xCB» .
«\xCC\xCD\xCE\xCF\xD0\xD1\xD2» .
«\xD3\xD4\xD5\xD6\xD8\xD9\xDA» .
«\xDB\xDC\xDD\xDE\x9F» );
return $str ;
>
?>
So, this function changes also other letters into uppercase, ucfirst() does only change: a-z to: A-Z.
Note: the return for this function changed in versions 4.3 when a string is passed of length 0. In <4.2 false is returned and in >4.3 a string of length 0 is returned.
Results for <4.2:
bool(false) string(4) «Owen»
Results for >4.3:
string(0) «» string(4) «Owen»
In the event you sort of need multiple delimiters to apply the same action to, you can preg_replace this «second delimiter» enveloping it with your actual delimiter.
A for instance, would be if you wanted to use something like Lee’s FormatName function in an input box designed for their full name as this script was only designed to check the last name as if it were the entire string. The problem is that you still want support for double-barreled names and you still want to be able to support the possibility that if the second part of the double-barreled name starts with «mc», that it will still be formatted correctly.
This example does a preg_replace that surrounds the separator with your actual delimiter. This is just a really quick alternative to writing some bigger fancier blah-blah function. If there’s a shorter, simpler way to do it, feel free to inform me. (Emphasis on shorter and simpler because that was the whole point of this.) 😀
Here’s the example. I’ve removed Lee’s comments as not to confuse them with my own.
function FormatName ( $name = NULL )
<
if (empty( $name ))
return false ;
$name = strtolower ( $name );
$name = preg_replace ( «[\-]» , » — » , $name ); // Surround hyphens with our delimiter so our strncmp is accurate
if ( preg_match ( «/^[a-z]<2,>$/i» , $name )) // Simple preg_match if statement
<
$names_array = explode ( ‘ ‘ , $name ); // Set the delimiter as a space.
for ( $i = 0 ; $i < count ( $names_array ); $i ++)
<
if ( strncmp ( $names_array [ $i ], ‘mc’ , 2 ) == 0 || ereg ( ‘^[oO]\'[a-zA-Z]’ , $names_array [ $i ]))
<
$names_array [ $i ][ 2 ] = strtoupper ( $names_array [ $i ][ 2 ]);
>
$names_array [ $i ] = ucfirst ( $names_array [ $i ]);
$name = implode ( ‘ ‘ , $names_array );
$name = preg_replace ( «[ \- ]» , «-» , $name ); // Remove the extra instances of our delimiter
return ucwords ( $name );
Сделать первую букву заглавной (PHP)
В PHP есть функция ucfirst() , которая первую букву в строке делает заглавной, ucwords() — делает заглавными буквы во всех словах строки, в работе с кириллицей, в юникоде, возникают проблемы.
Кириллица и юникод — вечная проблема всех версий PHP, частично проблема решена, существует функция string mb_convert_case (string str, int mode [, string encoding]) , которая принимает в качестве параметров строку, режим преобразования (0 — все буквы в верхний регистр, 1 — все буквы в нижний регистр, 2 — ВСЕ ПЕРВЫЕ буквы всех слов в верхний регистр) и кодировка.
Преобразование букв
Задача: преобразовать первую букву в строке и все первые буквы во всех словах в строке.
Английские буквы
С английскими буквами в стандартно-используемых кодировках (UTF-8 и Windows-1251) проблем не возникает.
Результат на экране
First letters
First Letters
Кириллица и Windows-1251
С кириллицей в Windows-1251 проблем так же не должно возникнуть.
Результат на экране
Первые буквы
Первые Буквы
Кириллица и UTF-8
С кириллицей в юникоде функции ucfirst() и ucwords() не справятся и преобразований не произойдет.
Для этого определяется функция mb_ucfirst(string str [, string encoding]) , которая будет обрабатывать юникод-строки.
Результат на экране
первые буквы
первые буквы
Первые буквы
Первые Буквы
Категории
Читайте также
Комментарии
Перед тем как подобные статьи писать, разберись сам в функциях. Ничего о не меняет, как были слова так и остались
Замена регистра в строках PHP
Список PHP-функций для изменения регистра символов в строках и примеры их использования.
Проверка, является ли буква прописной или строчной
Функция ctype_upper($string) – определяет, являются ли все буквы в строке в верхнем регистре.
Вариант для кириллицы в кодировке UTF-8:
Пример определения регистра для первой буквы в строке:
Первая заглавная буква
ucfirst($string) — преобразует первый символ строки в верхний регистр.