Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the space between two words in PHP [duplicate]

Tags:

string

php

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.

like image 428
Nilesh Patil Avatar asked Nov 28 '22 06:11

Nilesh Patil


2 Answers

<?php
    $str = "Paneer Pakoda dish";
    echo str_replace(' ', '', $str);
?> 
like image 161
Santosh Patel Avatar answered Nov 30 '22 20:11

Santosh Patel


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

like image 22
Shashank Shah Avatar answered Nov 30 '22 21:11

Shashank Shah