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

青灯夜游
Release: 2023-03-09 17:40:02
Original
2589 people have browsed it

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)
Copy after login

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;
?>
Copy after login

Output:

Hello World!
HELLO WORLD!
Hello World!
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template