PHP Remove string end-to-end Chinese and English space program _php tutorial

Source: Internet
Author: User
Tags rtrim
The following article to give you a summary of a number of PHP to remove the string end-to-end Chinese and English space program examples, here useful regular replacement and trim series function Delete, below we take a look.

Example 1.trim function to remove a space


The trim () function is used to remove spaces at the beginning and end of a string, and returns a string after the space is stripped. The syntax is as follows:
String Trim (String str[,string charlist]);
The LTrim () function is used to remove spaces to the left of a string or to specify a string. The syntax is as follows:
String LTrim (String str[,string charlist]);
The RTrim () function is used to remove spaces and special characters to the right of a string. The syntax is as follows:
String RTrim (String str[,string charlist]);

The code is as follows Copy Code

$a = "(A,b,c,)";
echo $a. "
"; Output: (A,b,c,)
$b =trim ($a, "()"); Remove the character "(" or ")" from the end of the string
echo $b. "
"; Output: A,b,c,
$c =trim ($a, "(,)"); Remove the character "(", "," or ")" from the end of the string
echo $c. "
"; Output: A,b,c

?>


Output Result:
(A,b,c,)
A,b,c,
A,b,c
In SQL, function trim () is used to remove the leading and trailing spaces, LTrim () to remove the left space of the string, and RTrim () to remove the right space from the string.

Example 2. Using the Str_replace regular replacement

The code is as follows Copy Code

function Mbtrim ($STR)
{
Return Mb_ereg_replace (' (^ (|) +| | +$) ', ', $str);
}

Example 3.str_replace Regular replacement

The code is as follows Copy Code

$str = "www.bKjia.c0m";
$str = Mb_ereg_replace (' ^ (|) + ', ', $str);
$str = Mb_ereg_replace (' (|) +$ ', ', $str);
Echo mb_ereg_replace (', ' n ', $str);
?>

http://www.bkjia.com/PHPjc/632753.html www.bkjia.com true http://www.bkjia.com/PHPjc/632753.html techarticle the following article to give you a summary of a number of PHP to remove the string end-to-end Chinese and English space program examples, here useful regular replacement and trim series function Delete, below we take a look. Example ...

  • 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.