php 去掉PHP中两个单词之间的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34735829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove the space between two words in PHP
提问by Nilesh Patil
I am developing a website in PHP. In it, I am saving the images in a folder on a server.
我正在用 PHP 开发一个网站。在其中,我将图像保存在服务器上的文件夹中。
I accept a name from user and want to use that name as the image name. Sometimes the user enters a name like two words.
我接受用户的名称并希望使用该名称作为图像名称。有时用户输入一个名字,就像两个词一样。
So I want to remove the space between two words. For example, if the user enters as 'Paneer Pakoda dish', I want to convert it like 'PaneerPakodaDish'.
所以我想删除两个单词之间的空格。例如,如果用户输入为“Paneer Pakoda Dish”,我想将其转换为“PaneerPakodaDish”。
How can I do that?
我怎样才能做到这一点?
I used
我用了
1) str_replace(' ', '', $str);
2) preg_replace(' ', '', $str);
3) trim($str, ' ');
But these are not giving the output as I required.
但是这些并没有按照我的要求提供输出。
回答by Santosh Patel
<?php
$str = "Paneer Pakoda dish";
echo str_replace(' ', '', $str);
?>
回答by Amit Shah
The code below should work
下面的代码应该工作
<?php
$test = "My Name is Amit";
echo preg_replace("/\s+/", "", $test);
?>
回答by Shashank Shah
'PaneerPakodaDish' should be the desired output.
'PaneerPakodaDish' 应该是所需的输出。
$string = 'Paneer Pakoda dish';
$s = ucfirst($string);
$bar = ucwords(strtolower($s));
echo $data = preg_replace('/\s+/', '', $bar);
It will give you the exact output 'PaneerPakodaDish' where character "D" will also be in capital.
它将为您提供准确的输出“PaneerPakodaDish”,其中字符“D”也将大写。
回答by Divakarcool
<?php
$char = "Lorem Ipsum Amet";
echo str_replace(' ', '', $char);
?>
The result will look like this: LoremIpsumAmet
结果将如下所示:LoremIpsumAmet
回答by Butterfly
You may use
您可以使用
echo str_replace(' ', '', $str);
trim()
should be used to remove white space at the front and back end of the string.
trim()
应该用于删除字符串前端和后端的空白。
回答by Jalpa
Please try "preg_replace" for remove space between words.
请尝试“preg_replace”删除单词之间的空格。
$string = "Paneer Pakoda dish";
$string = preg_replace('/\s+/', '', $string);
echo $string;