Home > Back-end >  Add double quote to a string
Add double quote to a string

Time:08-25

How can I add double quote to this string. The string I have

01-08-2022,02-08-2022,04-08-2022,05-08-2022,06-08-2022,14-08-2022

The string I want

"01-08-2022","02-08-2022","04-08-2022","05-08-2022","06-08-2022","14-08-2022"

CodePudding user response:

Try with using explode and implode method as below

$oldString = '01-08-2022,02-08-2022,04-08-2022,05-08-2022,06-08-2022,14-08-2022';
$newString = sprintf('"%s"', implode('","', explode(',', $oldStr)));
echo $newString;

CodePudding user response:

As an alternative to CBroe's solution in the comment, you can also work with str_replace:

$str = '01-08-2022,02-08-2022,04-08-2022,05-08-2022,06-08-2022,14-08-2022';

$str = '"'.str_replace(',','","',$str).'"';

echo $str;
//"01-08-2022","02-08-2022","04-08-2022","05-08-2022","06-08-2022","14-08-2022"

CodePudding user response:

manual add, you can just add back slash (\") before double quote to show double quote at output. ex: "\"01-08-2022\",\"02-08-2022\""

output

"01-08-2022","02-08-2022"

complete explatation check here: https://www.digitalocean.com/community/tutorials/how-to-work-with-strings-in-php

or you can split string with comma (,) and then join again plus add "\"" splitString "\""

may it help.

  • Related