Php delete left-side and right-side space functions _ PHP Tutorial

Source: Internet
Author: User
Php deletes the left-side and right-side space functions. Deleting functions in php is much more specific than js. in addition to the trim () function, there are also ltrim () and rtrim () functions. they need to delete spaces between front and back, respectively, in addition to the three functions, you can also use the positive deletion function in php, which is much more specific than js. in addition to the trim () function, there are also ltrim () and rtrim () functions, they need to delete spaces between the front and back of each other. in addition to the three functions, they can also use regular expression deletion.

Ltrim () function

Ltrim ($ str, $ charlist)
// $ Str indicates the string to be processed;
// $ Charlist is a special character to be deleted. if it is null, spaces on the left are removed.

The code is as follows:

$ T = "... I'm Jacky ...";
Echo "a". $ t ."
";
$ Left = ltrim ($ t );
Echo "a". $ left ."
";
$ Lleft = ltrim ($ left ,".");
Echo $ lleft;
?>

Rtrim () function


Rtrim ($ str, $ charlist)
// $ Str indicates the string to be processed;
// $ Charlist is a special character to be deleted. if it is null, spaces on the right are removed.

The code is as follows:

$ A = "htm ";
Echo $ a. "l "."
";
Echo rtrim ($ a). "l ";
?>

Trim () function

First remove leading and trailing spaces

The code is as follows:

$ Str = "This line containstliberal rn use of whitespace. nn ";

// First remove the leading and trailing spaces
$ Str = trim ($ str );

// Remove the two or more spaces


$ Str = preg_replace ('/s (? = S)/', '', $ str );

// Replace a non-space with a space.
$ Str = preg_replace ('/[nrt]/', '', $ str );

The preceding example can remove all unnecessary spaces. Use TRim () to remove leading and trailing spaces, and then use preg_replace () to remove repeated spaces.

Use regular expressions to replace and improve functions
Php removes spaces at the beginning and end of the string (including the full angle)

The code is as follows:

$ Str = "www.bkjia.com ";
$ Str = mb_ereg_replace ('^ (|) +', '', $ str );
$ Str = mb_ereg_replace ('(|) + $', '', $ str );
Echo mb_ereg_replace (''," n ", $ str );
?>


The trim () function, as well as the ltrim () and rtrim () functions, respectively. they need to delete the spaces between the front and back. in addition to the three functions, they can also use the positive...

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.