Php: How to delete spaces in the middle of a string _ PHP Tutorial

Source: Internet
Author: User
In php, there are three methods to delete spaces in the middle of a string. First, use the regular expression to copy the code as follows :? Phpechopreg_replace (##,, abab); output abab? Type 2: Use the str_replace () function to copy the code as follows :? First: use regular expressions

The code is as follows:

Echo preg_replace ('#', '', 'AB ');
// Output "abab"
?>


Method 2: Use the str_replace () function

The code is as follows:

Echo str_replace ('','', 'AB AB ');
// Output "abab'
?>


Third: use the strtr () function

The code is as follows:

Echo strtr ('AB AB', array (''=> ''));
// Output "abab"
?>


Strtr () functions are used in a special way, essentially:

The code is as follows:

Strtr ('ewb ', 'web', '20140901') =
Strtr ('ewb ', array ('E' => '2', 'W' => '1',' B '=> '3') =
Str_replace (array ('e', 'W', 'B'), array ('2', '1', '3'), 'ewb ');
?>

Type 4: Use encapsulated functions

The code is as follows:


Function trimall ($ str) // delete spaces
{
$ Qian = array ("", "", "\ t", "\ n", "\ r ");
$ Hou = array ("","","","","");
Return str_replace ($ qian, $ hou, $ str );
}

The pipeline code is as follows :? Php echo preg_replace ('#', '', 'AB AB'); // output" abab "? Type 2: Use the str_replace () function code as follows :?...

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.