PHP Delete the left and right side of the space method, php deleted _php Tutorial

Source: Internet
Author: User
Tags rtrim

PHP Delete the left and right side of the space method, PHP remove Blank Space


This article explains how PHP removes the left and right spaces. Share to everyone for your reference. Here's how:

In PHP to delete the function is more specific than JS, in addition to the trim () function, there are LTrim () and RTrim () function, they have to delete the left and right space, in addition to these three functions can also use regular delete.

LTrim () function: LTrim ($STR, $charlist)

$STR represents the string being processed, $charlist is the special character to be removed, and if empty, the left-hand space is removed and the code is as follows:
Copy the Code code 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 represents the string being processed, $charlist is the special character to be removed, and if empty, the right-hand space is removed, and the code is as follows:
Copy the Code code as follows: <?php
$a = "htm";
echo $a. " L "."
";
echo RTrim ($a). " L ";
?>

trim () function, first to turn the trailing space, the code is as follows:
Copy the code as follows: $str = "This line containstliberal RN use of whitespace.nn";
First, turn the trailing blanks.
$str = Trim ($STR);
Then remove more than two spaces above the
$str = Preg_replace ('/s (? =s)/', ' ', $str);
Finally, replace the non-whitespace with a space
$str = preg_replace ('/[nrt]/', ' ', $str);
Using the above example, you can remove all the extra spaces, first use trim () to go to the leading and trailing blanks, and then use Preg_replace () to remove the repeated spaces.

by replacing the regular expression , the function is stronger, PHP removes the string from the end of the line (including the full width), the code is as follows:
Copy the Code code as follows: <?php
$STR = "Help the house www.jb51.net";
$str = Mb_ereg_replace (' ^ (|) + ', ', $str);
$str = Mb_ereg_replace (' (|) +$ ', ', $str);
Echo mb_ereg_replace (', ' n ', $str);
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/919627.html www.bkjia.com true http://www.bkjia.com/PHPjc/919627.html techarticle PHP Delete the left and right side of the space method, PHP remove blank space In this example, this article describes the method of removing the empty and right end of PHP. Share to everyone 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.