删除PHP中两个单词之间的空格[复制]
This question already has an answer here:
I am developing a website in PHP. In it, I am saving the images in a folder on a server.
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'.
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.
</div>
此问题已经存在 这里有一个答案: p>
-
如何在PHP中删除字符串中的所有空格? [复制]
4 answers
span>
li>
\ n ul>
div>
我正在用PHP开发一个网站。 在其中,我将图像保存在服务器上的文件夹中。 p>
我接受用户的名称并希望使用该名称作为图像名称。 有时用户输入两个单词的名称。 p>
所以我想删除两个单词之间的空格。 \例如,如果用户输入'Paneer Pakoda菜',我想 将其转换为'PaneerPakodaDish'。 p>
我该怎么做? p>
我用过 p>
1)str_replace('','',$ str); 2)preg_replace('','',$ str); 3)trim($ str,''); code> pre>
但这些并没有按照我的要求提供输出。 p> div>
'PaneerPakodaDish' should be the desired output.
$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.
The code below should work
<?php
$test = "My Name is Amit";
echo preg_replace("/\s+/", "", $test);
?>
<?php
$str = "Paneer Pakoda dish";
echo str_replace(' ', '', $str);
?>
You may use
echo str_replace(' ', '', $str);
trim()
should be used to remove white space at the front and back end of the string.
Please try "preg_replace" for remove space between words.
$string = "Paneer Pakoda dish";
$string = preg_replace('/\s+/', '', $string);
echo $string;
<?php
$char = "Lorem Ipsum Amet";
echo str_replace(' ', '', $char);
?>
The result will look like this: LoremIpsumAmet