Search code examples
phpjoinexplodeimplodetext-manipulation

How to manipulate text in PHP to have single quote and coma


My question must be simple but I can't figure how to do that:

$input = "Hello, Beautiful, World";

and

$expected_output = "'Hello','Beautiful','World'";

I know I can split text by explode(" ", $input);

but how to join with ', ?

Why I need it? I need to have it to prepare MySQL query like

SELECT value FROM tab_settings WHERE name IN ('Hello', 'Beautiful', 'World')

from $input


Solution

  • You can use below snippet for the same

    echo "'".implode("','",explode(", ", $input))."'";
    

    Demo