Search code examples
phpstringdouble

Converting Comma Separated Value to Double Quotes comma separated string


I have a comma separated value like

alpha,beta,charlie

how can I convert it to

"alpha","beta","charlie"

using a single function in php without using str_replace?


Solution

  • Alternatively to Richard Parnaby-King's function (shorter):

    function addQuotes($string) {
        return '"'. implode('","', explode(',', $string)) .'"';
    }
    
    echo addQuotes('alpha,beta,charlie'); // = "alpha","beta","charlie"