问题
Ive been looking around for a way in PHP to have a string converted, so that the first letter becomes uppercase and the rest lower case.
At the moment I am doing what I believe is the standard way:
ucfirst(strtolower($string));
But I have found that some programming languages (ie. tcl) can do it with one cammand:
totitle
Is there a way to do this in PHP? It's not a problem as such, I'm just a curios dude :D
Thanks
回答1:
function totitle($string){
return ucfirst(strtolower($string));
}
And voila :)
回答2:
You should go like this
<?php
$string= 'HELLO WORLD';
$string = strtolower($string);
$string = ucfirst($string);
?>
回答3:
"Title" casing capitalizes each word in a string (i.e. every letter following white space). Your approach would result in "Gone with the wind," whereas title casing would yield "Gone With The Wind".
I wouldn't worry about it: what you're doing is simple and intuitive, and if it gets you what you want, there's not any intrinsic functions that do the same thing.
回答4:
You can create such function yourself.
Do not forget that you should use mb_*
functions for data that was input by user. English is not the only language people use. Look at this question: ucfirst() function for multibyte character encodings
来源:https://stackoverflow.com/questions/21484715/uppercase-first-letter-and-rest-lower