Home  >  Article  >  Backend Development  >  PHP English letter case conversion function

PHP English letter case conversion function

WBOY
WBOYOriginal
2016-07-25 09:13:10997browse

Example 1, convert the first letter of each word to uppercase: ucwords()

  1. $foo = 'hello world!';
  2. $foo = ucwords($foo); // Hello World!
  3. $bar = 'HELLO WORLD!';
  4. $bar = ucwords ($bar); // HELLO WORLD!
  5. $bar = ucwords(strtolower($bar)); // Hello World!
  6. ?>
Copy code

Example 2, the first letter of the first word changes Uppercase: ucfirst()

  1. $foo = 'hello world!';
  2. $foo = ucfirst($foo); // Hello world!
  3. $bar = 'HELLO WORLD!';
  4. $bar = ucfirst ($bar); // HELLO WORLD!
  5. $bar = ucfirst(strtolower($bar)); // Hello world!
  6. ?>
Copy code

Example 3, the first letter of the first word changes Lowercase: lcfirst()

  1. $foo = 'HelloWorld';
  2. $foo = lcfirst($foo); // helloWorld
  3. $bar = 'HELLO WORLD!';
  4. $bar = lcfirst($bar) ; // hELLO WORLD!
  5. $bar = lcfirst(strtoupper($bar)); // hELLO WORLD!
  6. ?>
  7. Change all letters to uppercase: strtoupper()
  8. Change all letters to lowercase: strtolower()
Copy Code

The above introduces a total of five English letter case conversion functions, namely ucwords, ucfirst, lcfirst, strtoupper, and strtolower, each with its own use.



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