Home >Backend Development >PHP Problem >How to convert the first letter of a word in a php statement to capitalized

How to convert the first letter of a word in a php statement to capitalized

青灯夜游
青灯夜游Original
2021-05-14 17:47:592585browse

In PHP, you can use the ucwords() function to convert the first letter of a word to uppercase; the function of this function is to convert the first letter of each word in the string to uppercase, and its syntax is "ucwords(str )", its parameter str represents the string to be converted. When using it, you only need to pass the string into str.

How to convert the first letter of a word in a php statement to capitalized

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, you can use ucwords () function converts the first letter of a word to uppercase.

ucwords() function converts the first character of each word in a string to uppercase.

Note: This function is binary safe.

Syntax

ucwords(string)

Parameter string: required. Specifies the string to be converted.​

Return value: Returns the converted string.

Code example:

<?php
$foo = &#39;hello world!&#39;;
$foo = ucwords($foo);             // Hello World!
echo $foo."<br>";

$bar = &#39;HELLO WORLD!&#39;;
$bar = ucwords($bar);             // HELLO WORLD!
echo $bar."<br>";
$bar = ucwords(strtolower($bar)); // Hello World!
echo $bar;
?>

Output:

Hello World!
HELLO WORLD!
Hello World!

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert the first letter of a word in a php statement to capitalized. For more information, please follow other related articles on the PHP Chinese website!

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