How to delete left and right-side spaces in PHP _php tips

Source: Internet
Author: User
Tags rtrim

This article illustrates the way PHP deletes left and right-end spaces. Share to everyone for your reference. The specific methods are as follows:

In PHP to delete a function than JS more specific, in addition to the trim () function, there are LTrim () and RTrim () function, they want to delete the space around, in addition to these three functions can also be used to delete.

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

The $STR represents the string being processed, $charlist is the special character to be removed, and if it is empty, the left side of the space is removed and the code is as follows:

Copy Code code as follows:
<?php
$t = "... I ' m Jacky ... ";
echo "a". $t. " <br> ";
$left =ltrim ($t);
echo "a". $left. " <br> ";
$lleft =ltrim ($left, ".");
Echo $lleft;
?>

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

The $STR represents the string being processed, $charlist is the special character to be deleted, and if it is empty, the right side of the space is removed and the code is as follows:

Copy Code code as follows:
<?php
$a = "htm";
echo $a. " L "." <br> ";
echo RTrim ($a). " L ";
?>

trim () function, first go to the tail space, the code is as follows:

Copy Code code as follows:
$STR = "This line containstliberal RN with Whitespace.nn";
First go to the tail space
$str = Trim ($STR);
Then remove the two spaces above the
$str = Preg_replace ('/s (? =s)/', ', $str);
Finally, replace the non-space with a space
$str = preg_replace ('/[nrt]/', ', ', $str);

Use the example above to remove all the extra spaces, first using trim () to the ends of the space, and then using Preg_replace () to remove the duplicate spaces.

with regular expression replacement , more powerful, PHP to remove the end of the string space (including the full corner), the code is as follows:

Copy Code code as follows:
<?php
$STR = "cloud-dwelling community 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 your PHP program design.

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.