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); }
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.