Home  >  Article  >  Backend Development  >  Letter case conversion function in php

Letter case conversion function in php

高洛峰
高洛峰Original
2016-11-29 11:27:541332browse

The letter case conversion functions in php include: strtolower, strtoupper, ucfirst, ucwords and other functions. This article will introduce to you how to use these letter case conversion functions.

1. Convert the string into Lowercase

strtolower(): This function converts all characters of the incoming string parameter to lowercase and puts it back into the string in small definite form. The code is as follows:

echo strtolower("Hello WORLD!");

2. Convert characters to uppercase

strtoupper(): The function of this function is opposite to the strtolower function. It converts all the characters of the passed character parameter into uppercase and returns the string in uppercase. The usage is the same. The same as strtolowe(), the code is as follows:

$str = "Mary Had A Little Lamb and She LOVED It So";

$str = strtoupper($str);

echo $str; / / Print MARY HAD A LITTLE LAMB AND SHE LOVED IT SO

?>

3. Convert the first character of the string to uppercase

ucfirst(): The function of this function is to change the first character of the string to Uppercase, this function returns a string with the first character in uppercase. The usage is the same as strtolowe(), the code is as follows:

$foo = 'hello world!';

$foo = ucwords($foo); // Hello World!

//Open source code phpfensi.com

$bar = 'HELLO WORLD!';

$bar = ucwords($bar); // HELLO WORLD!

$bar = ucwords(strtolower( $bar)); // Hello World!

?>

4. Convert the first character of each word in the string to uppercase

ucwords(): This function converts the first character of each word in the string passed in The first character becomes uppercase. For example, "hello world", after being processed by this function, "Hello Word" will be returned. The usage is the same as strtolowe(), the code is as follows:

$foo = 'hello world! ';

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

$bar = 'HELLO WORLD!';

$bar = ucfirst($bar); // HELLO WORLD!

$bar = ucfirst(strtolower($bar)); // Hello world!

?>

5. The first letter of the first word is lowercase lcfirst(), the code is as follows:

$foo = 'HelloWorld ';

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

$bar = 'HELLO WORLD!';

$bar = lcfirst($bar); // hELLO WORLD!

$bar = lcfirst( strtoupper($bar)); // hELLO WORLD!

?>


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