Hướng dẫn
Quảng cáo

Hàm ucwords() trong PHP

Hướng dẫn cách sử dụng hàm ucwords() trong lập trình PHP

Tác dụng của hàm ucwords()

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

The following table summarizes the technical details of this function.

Return Value: Returns the modified string.
Version: PHP 4+

Syntax

The basic syntax of the ucwords() function is given with:

ucwords(string, separators);

The following example shows the ucwords() function in action.

<?php
// Sample string
$str = "hello world!";

// Making first character in each word uppercase
echo ucwords($str);
?>

Here's a list of the functions related to ucwords() function:

  • lcfirst() – Converts the first character of a string to lowercase.
  • ucfirst() – Converts the first character of a string to uppercase.
  • strtoupper() – Converts a string to uppercase.
  • strtolower() – Converts a string to lowercase.

Parameters

The ucwords() function accepts the following parameters.

Parameter Description
string Required. Specifies the string to work on.
separators Optional. Specifies the word separator characters.

Tip: The definition of a word is any string of characters that is immediately after any character listed in the separators parameter. The default separator characters are: space (" "), form-feed ("\f"), newline ("\n"), carriage return ("\r"), horizontal tab ("\t"), and vertical tab ("\v").


More Examples

Here're some more examples showing how ucwords() function actually works:

In the following example first character of each comma separated word will be capitalized.

<?php
// Sample string
$str = "apple,banana,orange,papaya";

// Capitalize first character of each word
echo ucwords($str, ",");
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Advertisements