The split and merge SQL statement string contains an SQL string, $ SQL & nbsp; & quot; INSERT & nbsp; INTO & nbsp; tablename & nbsp; (field1, field2, field3, field4, field5, field6, field7, field8) & nbsp; VALUES & nbsp; (& #3 split and merge SQL statement strings
There is an SQL string,
$ SQL = "INSERT INTO tablename (field1, field2, field3, field4, field5, field6, field7, field8) VALUES ('b9', NULL, 'he, P', 1, 'D-0', '4D ')";
The VALUES section has been taken out and put into string variables, such as: $ v = "'b9', NULL, 'he, P', 1, 'D-0, '4D '"
Q: How can I split $ v into an array and then merge it into an array? (Single quotes and commas are required)
------ Solution --------------------
Vomit. I didn't see your needs clearly.
Confirm the data format
Try the following delimiter
$ V = "'b9', NULL, 'he, P', 1, 'D-0', '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', '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);
?>