0

I have a string such as this "$names = "name1,name2".

I am going to be writing these names into an SQL IN statement such as "... IN('name1','name2')";

How can I seperate the "name1,name2" into 'name1' , 'name2'?

This is essential for the SQL statement to work correctly.

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42

1 Answers1

1

Was able to use explode and implode as described by the comments. Then ran into the problem of a space before one of my first names, where I had to remove the whitespace.

Works 100%!

 foreach ($usernames as $details) {
        $names = $details->meta_value;
        $sortednames = preg_replace('/\s*,\s*/', ',', $names);
        $explodednames = explode(',',$sortednames);
        echo "explodednames".$explodednames;
        $implodednames = implode("','",$explodednames);
        }