6

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nilesh Patil
  • 138
  • 1
  • 1
  • 10

6 Answers6

18
<?php
    $str = "Paneer Pakoda dish";
    echo str_replace(' ', '', $str);
?> 
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Santosh Patel
  • 549
  • 2
  • 13
6

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shashank Shah
  • 2,077
  • 4
  • 22
  • 46
4

The code below should work

<?php
    $test = "My Name is Amit";
    echo preg_replace("/\s+/", "", $test);
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amit Shah
  • 1,380
  • 1
  • 10
  • 19
2
<?php
    $char = "Lorem Ipsum Amet";
    echo str_replace(' ', '', $char);
?>

The result will look like this: LoremIpsumAmet

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Divakarcool
  • 473
  • 6
  • 20
0

You may use

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

trim() should be used to remove white space at the front and back end of the string.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anto S
  • 2,448
  • 6
  • 32
  • 50
0

Please try "preg_replace" for remove space between words.

$string = "Paneer Pakoda dish";
$string = preg_replace('/\s+/', '', $string);
echo $string;
Jalpa
  • 697
  • 3
  • 13