How to explode a string and separate each value with single quotes and commas in PHP

The delimiter is a comma that separates each value in the string.

$image = "a.jpg,b.jpg,c.jpg";

I would like each value to output as 'a.jpg', 'b.jpg', 'c.jpg'

Each value within single quotes and separated by a comma.

This is my code so far:

$result=$db->singleQuery("SELECT * FROM units WHERE username=?sql AND id=?sql", Array($user,$id));

foreach ($result as $row)

$image = $row["images"];

echo $image;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
perhaps you can try:

$image = $row["images"];
$arr = explode(",", $image);
$image2 = "'".implode("','",$arr)."'";

echo $image." : ".$image2;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Ryan's solution is correct if there is only one row in your database, and given what little we know about your application.

You probably want to get the assistance of a database administrator to help you redesign the tables.  Each column of your database should have one and only one value.  If you have more than one image name in a column, you've violated that one-and-only-one principle, and you will wind up writing a lot of (potentially brittle and buggy) code to cover up the violation, such as this unnecessary exercise in string processing.  Instead of putting a list of image names in a column, you would have a separate "images" table with the image information listed in the rows.  Each row of "images" would have at a minimum three columns: an AUTO_INCREMENT KEY, a foreign key that identified the associated row of the "units" table, and a data column that gave the URL of the image file.  You could put other things into this "images" table, too, such as descriptive information, publishing credits, alt= text, etc.  The point is to keep the data normalized, atomic and canonical.  You JOIN these tables in your query.  Some further reading:

PHP foreach() is a control structure that influences the logical top-to-bottom flow of your code.  You want to use curly braces to show what instructions should be executed in the loop.  This is probably closer to what you want:
foreach ($result as $row)
    echo $row["images"];

Open in new window

I can't remember if I've mentioned it before, but we have a good article here at E-E for people who are just starting out in web development with PHP and MySQL.  It can help guide you to some good learning resources, and more importantly, steer you away from the many simple terrible examples of bad and outdated PHP code that litter the internet.  Classes, books and links are all mentioned; you will want to choose from a variety of different information sources.  All of these sources have been vetted by the PHP community and found to provide excellent guidance!
Genesis5150Author Commented:
Thanks Ryan that worked but is there a way to limit empty spaces? Not to show

'c.jpg', ''

Genesis5150Author Commented:
I actually found it:

$images = "'".implode("','",array_filter($arr))."'";

Thanks everyone
Genesis5150Author Commented:
It gave me what I was looking for plus adding array_filter() worked better by eliminating empty values
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.