Use regular expressions s to match spaces, always less match a space

Source: Internet
Author: User
$ABC = "TC,           M1"; $abc = Ereg_replace (", [\s]+", ",", $ABC); Echo $abc;


The above code is to remove the comma in the string str after the space, the expected result is "Tc,m1"
But the actual operation of the result is "TC, M1", between the comma and M1 more than one space, is why?


Reply to discussion (solution)

$ABC = ' TC,           M1 '; $abc = Preg_replace ("/,[\s]+/", ",", $ABC); echo "
$abc ";


Try, use preg_replace, the result is correct, there is no space, this is why

Ereg function Group is already in the abolition of the column, no need to tangle

$ABC = "TC,           M1"; $a =str_replace (",", $abc); Echo $a;

Tc,m1

Besides, Ereg has no \s.

It is recommended that you use Preg_replace, this function is faster.

Be sure to use ereg_replace, you can instead:
$ABC = Ereg_replace (", []+", ",", $ABC);

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