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

Hàm ltrim() trong PHP

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

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

The ltrim() function strips whitespace and other characters from the beginning of a string.

The following table summarizes the technical details of this function.

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


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

ltrim(string, strip_characters);

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

// Sample string
$str = "  Hello World! ";

// Stripping whitespace from the beginning
echo ltrim($str);


The ltrim() function accepts the following parameters.

Parameter Description
string Required. Specifies the string to be trimmed.

Optional. Specifies the list of all characters that you want to be stripped.

If this parameter is not specified, ltrim() will strip all the following characters:

  • " " - an ordinary space
  • "\t" - a tab
  • "\n" - a new line
  • "\r" - a carriage return
  • "\0" - the NULL-byte
  • "\v" - a vertical tab

More Examples

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

In the following example only spaces and tab character from the beginning of the string will be removed. The whitespace characters at the end and inside the string will remain intact.

// Sample string
$str = "  \tHello\n World!\n ";

// Trimming the leading whitespace
echo ltrim($str);

In the following example only space, plus, and minus from left end of the string will be removed.

// Sample string
$str = "  ++--\tHello World!++-- ";

// Removing characters from the left side
echo ltrim($str, " +-");

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

Bài viết mới