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>
'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