Splitting and merging SQL statement strings
has an SQL string,
$sql = "INSERT into tablename (FIELD1,FIELD2,FIELD3,FIELD4,FIELD5,FIELD6,FIELD7,FIELD8) VALUES (' B9 ', NULL, ' he,p ', 1, ' d -0 ', 1,11, ' 4d ') ";
The values section is now removed and put to a string variable such as: $v = "' B9 ', NULL, ' he,p ', 1, ' d-0 ', 1,11, ' 4d '"
Q: How do I split the $v into arrays and then merge the arrays or not? (Note single quotes and commas)
------Solution--------------------
Oh, I didn't see what you needed.
Please confirm the data format
Try the following split characters
$v = "' B9 ', NULL, ' he,p ', 1, ' d-0 ', 1,11, ' 4d '"
$t = Preg_split ('/, (?! \w+\ ',?) /I ', $v);
Print_r ($t);
Merge
echo implode (', ', $t);
------Solution--------------------
$v = "' B9 ', NULL, ' h,3,he,p ', 1, ' d-0 ', 1,11, ' 4d ';
$pattern = "/(' [^ ']* ') |,? ([^ ',]+),?/ ";
$res = Array ();
if (Preg_match_all ($pattern, $v, $match))
{
foreach ($match [1] as $key = $val)
{
if ($val = = ")
{
$res [$key] = $match [2][$key];
}
Else
$res [$key] = $val;
}
}
Echo '
Print_r ($res);
?