Php deletes spaces on the left and right sides. php deletes spaces on the left side. PHP Tutorial

Source: Internet
Author: User
Php deletes spaces on the left and right sides. php deletes spaces on the left side. Php deletes spaces on the left and right sides. This document describes how to delete spaces on the left and right sides of php. Share it with you for your reference. The specific method is as follows: php deletes spaces on the left side and on the right side. php deletes spaces on the left side.

This example describes how to delete spaces on the left and right sides of php. Share it with you for your reference. The specific method is as follows:

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 regular expressions to delete them.

Ltrim () function: ltrim ($ str, $ charlist)

$ Str indicates the processed string. $ charlist is a special character to be deleted. if it is null, spaces on the left are removed. the code is as follows:

The code is as follows:

<? Php
$ 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 processed string. $ charlist is a special character to be deleted. if it is null, spaces on the right are removed. the code is as follows:

The code is as follows:

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

Trim () function,First, remove the leading and trailing spaces. the code is as follows:

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 );


In the preceding example, we can remove all unnecessary spaces. First, we use TRim () to remove leading and trailing spaces, and then use preg_replace () to remove repeated spaces.

Replace with regular expressionsPhp removes spaces at the beginning and end of the string (including the full angle). The code is as follows:

The code is as follows:

<? Php
$ Str = "www.www.jb51.net ";
$ Str = mb_ereg_replace ('^ (|) +', '', $ str );
$ Str = mb_ereg_replace ('(|) + $', '', $ str );
Echo mb_ereg_replace (''," n ", $ str );
?>

I hope this article will help you with PHP programming.

Examples in this article describes how to delete spaces on the left and right sides of php. Share it with you for your reference. The specific method is as follows :...

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.