3

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,030
  • 21
  • 100
  • 124
Nilesh Patil
  • 108
  • 1
  • 1
  • 10

6 Answers6

13
<?php
    $str = "Paneer Pakoda dish";
    echo str_replace(' ', '', $str);
?> 
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Santosh Patel
  • 499
  • 2
  • 13
5

'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,030
  • 21
  • 100
  • 124
Shashank Shah
  • 1,921
  • 4
  • 18
  • 42
4

The code below should work

<?php
    $test = "My Name is Amit";
    echo preg_replace("/\s+/", "", $test);
?>
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Amit Shah
  • 1,272
  • 1
  • 9
  • 19
2
<?php
    $char = "Lorem Ipsum Amet";
    echo str_replace(' ', '', $char);
?>

The result will look like this: LoremIpsumAmet

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Divakarcool
  • 453
  • 5
  • 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,030
  • 21
  • 100
  • 124
Butterfly
  • 2,418
  • 5
  • 30
  • 49
0

Please try "preg_replace" for remove space between words.

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