简体   繁体   中英

How to strip only multiple whitespace in a string PHP

I want to remove only multiple white space in a string.

Example:

I am Mert Inal , so[multible white space here]I[multible white space here]can do that

It's must be:

I am Mert Inal soIcan do that

Try using preg_replace with the pattern \\s{2,} , and replace with empty string:

$input = "I am Mert Inal , so    I    can do that";
$output = preg_replace("/\s{2,}/", "", $input);
echo $output;

This outputs:

I am Mert Inal , soIcan do that

you can try

$string = "I am Mert Inal , so  I   ";

echo preg_replace('/\s+/', ' ',$string);

Output:

I am Mert Inal , so I .

Note:
/s is also applicable for \\n\\r\\t.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM