3 Ways to delete a space in the middle of a string in PHP

Source: Internet
Author: User

This article mainly introduces 3 kinds of methods in PHP to remove the space in the middle of the string, you need friends can refer to the following

The first: Use regular code as follows: <?php echo Preg_replace (' # # ', ', ' ab AB '); Output "Abab"?> the second: Use the Str_replace () function code as follows: <?php Echo str_replace (', ', ' ab AB '); Output "Abab '?> third: Use the STRTR () function code as follows: <?php Echo strtr (' Ab ab ', Array (' => ')); The output "Abab"?> strtr () function is somewhat special, essentially: The code is as follows: <?php strtr (' ewb ', ' web ', ' 123 ') = = Strtr (' ewb ', Array (' E ' => ' 2 ', ' W ') => ' 1 ', ' B ' => ' 3 ') = = Str_replace (Array (' E ', ' W ', ' B '), Array (' 2 ', ' 1 ', ' 3 '), ' ewb ');?> Fourth: Use the encapsulation function code as follows:     function Trimall ($STR)//delete space {$qian =array ("", "", "T", "N", "R");     $hou =array ("", "", "" "," "," ");  Return Str_replace ($qian, $hou, $STR); }

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.