简体   繁体   English

删除PHP中单词之间的空间

[英]Remove space in between words in php

How do I remove space between two words in php. 如何删除php中两个单词之间的空格。 For example 'Hello World' should return 'HelloWorld' . 例如, 'Hello World'应返回'HelloWorld' I tried using trim() but it did not work. 我尝试使用trim()但是没有用。

您可以使用str_replace

$str = str_replace(' ', '', $str);

您也可以这样做

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

trim used for remove spaces at begin and end of string. trim用于删除字符串开头和结尾的空格。 look here 这里

try it 试试吧

preg_replace("/\s+/", "", $str);

Complete code: 完整的代码:

$string="Hello World";
$string = str_replace(' ', '', $string);

Try this: 尝试这个:

function space_trim($st) {
        return @str_replace(" ","",$st);
    }
    echo space_trim("   a fff     jjj    ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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