How to delete spaces at the beginning and end of a character in php

Source: Internet
Author: User

Instance

The code is as follows: Copy code

<?
$ Str = "webpage creation tutorial www.111cn.net ";
$ Str = mb_ereg_replace ('^ (|) +', '', $ str );
$ Str = mb_ereg_replace ('(|) + $', '', $ str );
Echo mb_ereg_replace (''," n ", $ str );
?>

Some friends may not understand mb_ereg_replace (). Let's introduce the function mb_ereg_replace.

Mb_ereg_replace () we only need to pay attention to the above mb. Some friends can see that this function supports Chinese characters after reading character conversion.

Some may ask if php has its own function. Let's look at the example below.

Instance

The code is as follows: Copy code

<? Php
$ Str = "Remove leading and trailing spaces ";
Echo "the original string is in square brackets: [". $ str. "] <br> ";
Echo "original string length:". strlen ($ str). "<br> ";
$ Str1 = ltrim ($ str );
Echo "length after ltrim () execution:". strlen ($ str1). "<br> ";
$ Str2 = rtrim ($ str );
Echo "length after rtrim () execution:". strlen ($ str2). "<br> ";
$ Str3 = trim ($ str );
Echo "length after trim () execution:". strlen ($ str3). "<br> ";
Echo "remove the string after the beginning and end spaces: [". $ str3. "]";
?>

Summary: there are multiple solutions to the same problem. Just like deleting spaces, we can use two different methods to achieve the same effect.

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.