I have a variable with string value of 'laptop,Bag'
and I want it to look like ' "laptop","Bag" '
or "laptop","Bag"
. How could I do this one? Is there any php function that could get this job done? Any help please.

- 1,256
- 4
- 29
- 59
-
6did you even try to do it, it seems pretty much straight forward! – mamdouh alramadan Feb 21 '14 at 15:42
-
Yes, I have tried it using implode function but I couldn't make it work – Eli Feb 21 '14 at 15:43
-
1**Show us what you have tried** and then we can help you fix what you've done wrong. – Andy Lester Feb 21 '14 at 15:48
7 Answers
This would work. It first, explodes the string into an array. And then implodes it with speech marks & finishes up by adding the opening & closing speech mark.
$string = "laptop,bag";
$explode = explode(",", $string);
$implode = '"'.implode('","', $explode).'"';
echo $implode;
Output:
"laptop","bag"

- 2,677
- 4
- 22
- 35
That's what str_replace
is for:
$result = '"'.str_replace(',', '","', $str).'"';

- 214,931
- 59
- 362
- 292
-
-
I agree that this would be the more simple method of creating the string. No need to explode or implode anything, and no need to create another function. – Joe Feb 21 '14 at 15:48
This would be very easy to do.
$string = 'laptop,bag';
$items = explode(',', $string);
$newString = '"'.implode('","', $items).'"';
That should turn 'laptop,bag'
into "laptop","bag"
.
Wrapping that in a function would be as simple as this:
function changeString($string) {
$items = explode(',', $string);
$newString = '"'.implode('","', $items).'"';
return $newString;
}

- 1,384
- 10
- 17
-
1
-
1Ya, exactly the same thing haha! I provided the function with mine though! :) – Joe Feb 21 '14 at 15:47
I think you can explode
your string as array and loop throw it creating your new string
function create_string($string)
{
$string_array = explode(",", $string);
$new_string = '';
foreach($string_array as $str)
{
$new_string .= '"'.$str.'",';
}
$new_string = substr($new_string,-1);
return $new_string;
}
Now you simply pass your string the function
$string = 'laptop,Bag';
echo create_string($string);
//output "laptop","Bag"

- 23,183
- 12
- 55
- 64
-
-
1Correct, my first thought was for looping, implode would be faster though – Fabio Feb 21 '14 at 15:46
For your specific example, this code would do the trick:
<?php
$string = 'laptop,bag';
$new_string = ' "' . str_replace(',', '","', $string) . '" ';
// $new_string: "laptop","bag"
?>
That code would also work if you had more items in that list, as long as they are comma-separated.

- 326
- 3
- 11
Use preg_replace()
:
$input_lines="laptop,bag";
echo preg_replace("/(\w+)/", '"$1"', $input_lines);
Output:
'"laptop","Bag"'

- 75,622
- 18
- 128
- 150

- 11,944
- 3
- 37
- 49
I think you can perform that using explode in php converting that string in to an array.
$tags = "laptop,bag";
$tagsArray = explode(",", $tags);
echo $tagsArray[0]; // laptop
echo $tagsArray[1]; // bag
Reference http://us2.php.net/manual/en/function.explode.php
related post take a look maybe could solve your problem.
How can I split a comma delimited string into an array in PHP?

- 1
- 1

- 662
- 8
- 13