Home  >  Article  >  Backend Development  >  Example of converting the first letter of a php string to upper and lower case_PHP tutorial

Example of converting the first letter of a php string to upper and lower case_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:48:511265browse

In php, if we want to convert characters to uppercase or lowercase, we can just use strtolower or strtoupper, but to convert the first letter to uppercase, we need to use ucwords related functions

Change the first letter to uppercase: ucwords()

The code is as follows
 代码如下 复制代码

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

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

Copy code

 代码如下 复制代码

$foo = 'hello world!';
$foo = ucfirst($foo); // Hello world!

$bar = 'HELLO WORLD!';
$bar = ucfirst($bar); // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
?>

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

$bar = 'HELLO WORLD!';
$bar = ucwords($bar); // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!
?>
 代码如下 复制代码

$foo = 'HelloWorld';
$foo = lcfirst($foo); // helloWorld

$bar = 'HELLO WORLD!';

The first letter of the first word is capitalized: ucfirst()

The code is as follows
Copy code
$foo = 'hello world!';
$foo = ucfirst($foo); // Hello world!
$bar = 'HELLO WORLD!';
$bar = ucfirst($bar); // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
?> The first letter of the first word is lowercase lcfirst()
The code is as follows Copy code
$foo = 'HelloWorld';
$foo = lcfirst($foo); // helloWorld
$bar = 'HELLO WORLD!'; http://www.bkjia.com/PHPjc/632754.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632754.htmlTechArticleIn php, if we want to convert characters to upper and lower case, we can just use strtolower or strtoupper, but the first letter is required To convert uppercase and lowercase we need to use ucwords related functions to change the first letter...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn