PHP removes whitespace characters from the left side of the string or other predefined character functions LTrim ()

Source: Internet
Author: User
Tags rtrim

Instance

Remove the characters to the left of the string:

<?php$str = "Hello world!"; Echo $str. "<br>"; Echo LTrim ($str, "Hello"); >

Definition and usage

The LTrim () function removes white space characters or other predefined characters to the left of the string.

Related functions:

RTrim ()-Removes the white space character or other predefined character to the right of the string.

Trim ()-removes white space characters or other predefined characters on either side of the string.

Grammar

LTrim (String,charlist)

Parameter description

string is required. Specifies the string to check.

Charlist is optional. Specifies which characters are removed from the string. If this argument is omitted, all of the following characters are removed:

"+"-NULL

"\ T"-tab

"\ n"-line break

"\X0B"-Vertical tab

"\ r"-Enter

""-Space

Technical details

Return value: Returns the modified string.

PHP version: 4+

Update log: In PHP 4.1, the charlist parameter has been added.

More examples

Example 1

To remove spaces to the left of a string:

<?php$str = "Hello world!"; echo "Without LTrim:". $str, echo "<br>", echo "with LTrim:". LTrim ($STR);? >

The HTML output of the above code is as follows (see source code):

<! DOCTYPE html>

The browser output of the above code is as follows:

Without Ltrim:hello world! With Ltrim:hello world!

Example 2

Remove the line break (\ n) to the left of the string:

<?php$str = "Nnnhello world!"; echo "Without LTrim:". $str, echo "<br>", echo "with LTrim:". LTrim ($STR);? >

The HTML output of the above code is as follows (see source code):

<! DOCTYPE html>

The browser output of the above code is as follows:

Without Ltrim:hello world! With Ltrim:hello world!

String Trim (String $str [, String $charlist])-removes whitespace characters (or other characters) from the beginning and end of a string

Trim () function when the second argument is empty, the default is to remove spaces, tabs, newline characters, carriage returns, vertical tabs, and so on, when the second argument is added

1) Trim (' \ ' string\ ', ' \ ' sg '); Final output: \ "Strin

2) Trim (' \ ' string\ ', ' \ ' sg '); Final output: \ "String\"

2) Trim (' \ ' string\ ', ' \ ' sg '); Final output: Trin

The trim () function takes precedence over whitespace characters from the first and the end of the character, and then filters out the given character to be removed (list), and also applies to the LTrim (), RTrim () function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.