问:$query1 = ' title = "test1" AND name = "test2 " ';
$query2 = ' title = "test1"';
$query3 = ' title = "test1" OR name = "test2"';
$query4 = ' title = "test1" AND name = "test2" OR name = "test3"';
$query5: 'title = bird and name = Zhang San "or name = test3"';
我想取出其中的字段和值,例如$ query1中的标题名称及其对应的值,
请给我一些建议。
答:如果您使用PHP
<?php
$query1 = ' title = "test1" AND name = "test2" and age="30" ';
$query2 = ' title = "test1"';
$query3 = ' title = "test1" OR name = "test2"';
$query4 = ' title = "test1" AND name = "test2" OR name = "test3 "';
preg_match_all("/(\w+)\s*=\s*\"(\s*\w+\s*)\"/u", $query4,$match);
print_r($match);
//Output $match [1] is the array of all fields, and $match [2] is the array of corresponding values of all fields. 1 and 2 correspond one by one, and $match [1] [0] = $match [2] [0], that is, title = "test1"
// Array
// (
// [0] => Array
// (
// [0] => title = "test1"
// [1] => name = "test2"
// [2] => name = "test3"
// )
// [1] => Array
// (
// [0] => title
// [1] => name
// [2] => name
// )
// [2] => Array
// (
// [0] => test1
// [1] => test2
// [2] => test3
// )
// )