我有sql语句作为字符串;
a = "insert into table1 values('value1','value2','value3')"
b = "insert into table2 values('value4','value5','value6')"
c = "insert into table3 values('value7','value8','value9')"
我需要分割它们的函数,并在语句中给出第一个值(PHP 5.6)
a = splitFunction("insert into table1 values('value1','value2','value3')")//a ='value1'
b = splitFunction("insert into table2 values('value4','value5','value6')")//b='value4'
c = splitFunction("insert into table3 values('value7','value8','value9')")//c='value7'
答案 0 :(得分:0)
您可以尝试以下代码:
$a = "insert into table1 values('value1','value2','value3')";
preg_match("#values\('([^']*)'#", $a, $res);
$a_value = $res[1];
var_dump($a_value);
尝试此代码(Ctrl + ENTER):http://www.tehplayground.com/#s8l9tArRr
答案 1 :(得分:0)
查看下面的代码,它将返回所需的结果:
function splitFunction($str)
{
$patterh = '~\'(.*?)\'~';
preg_match($patterh, $str, $matches);
return (isset($matches[1])) ? $matches[1]:'no match';
}
echo splitFunction("insert into table1 values('value1','value2','value3')"); //output: value1
echo splitFunction("insert into table1 values('value4','value2','value3')"); //output: value4
echo splitFunction("insert into table1 values('value6','value2','value3')"); //output: value6
echo splitFunction("insert into table1"); //output: no match