PHP将一串逗号分隔值转换为另一种格式,而不使用循环

In php, if I had a string of comma separated data like this which came from a database:

John,Paul,Ringo,George

how could I convert it to something like this, without using a for loop or any extra processing. Does php have a function that can do this?

$str = 'John','Paul','Ringo','George';

I currently split the string using explode and then reformat it. Anyway to achieve this without doing that?

The formatted string is sent to a SQL where it's used in a MySQL IN() function.

If you absolutely don't want to use explode() you could use

$str = 'John,Paul,Ringo,George';
$newStr = "'" . str_replace(",","','", $str) . "'";

You can use the explode and implode functions in PHP.

$names = explode(',', 'John,Paul,Ringo,George');
echo implode(',', $names);

I hope I got you right:

$str_1 = 'John,Paul,Ringo,George';
$str_2 = explode(',', $str_1);
$str_3 = implode('\',\'', $str_2);
$str_4 = '\''.$str_3.'\'';

echo $str_4;

Output: 'John','Paul','Ringo','George'

$l = 'John,Paul,Ringo,George';
$lx = "'" . implode("','", explode(",", $l)) . "'";
echo $lx; // 'John','Paul','Ringo','George'

You can use preg_replace with $1 thing.

UPDATED: See usage example:

echo preg_replace('((\\w+)(,?))', '"$1"$2', 'John,Paul,Ringo,George');

you can use explode like below

$db_data = 'John,Paul,Ringo,George';//from your db
$l = explode(',',$db_data);
echo "select * from table where column IN('".implode("','",$l)."')";

output:

select * from table where column IN('John','Paul','Ringo','George')