Uppercase first letter and rest lower

一曲冷凌霜 提交于 2019-12-02 22:52:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!