本文介绍了删除多余的空格,但不要删除两个单词之间的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想删除字符串中多余的空格.我已经尝试过trim
,ltrim
,rtrim
和其他命令,但是它们都不起作用,甚至尝试了以下内容.
I want to remove extra spaces present in my string. I have tried trim
,ltrim
,rtrim
and others but non of them are working and even tried the below stuffs.
//This removes all the spaces even the space between the words
// which i want to be kept
$new_string = preg_replace('/\s/u', '', $old_string);
对此有什么解决方案吗?
Is there any solution for this ?
已更新:-
输入字符串:-
"
Hello Welcome
to India "
输出字符串:-
"Hello Welcome to India"
推荐答案
$cleanStr = trim(preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $str)));
这篇关于删除多余的空格,但不要删除两个单词之间的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!