Tag:正则表达式
Article From:https://segmentfault.com/q/1010000012154384
Question:
$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 three" OR name = "test3";

I want to take out the fields and values in them, such as title name in $query1 and their corresponding values.
Please point out that the little brother is a slag.

Answer 0:

If you use 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]It is an array of all fields. $match[2] is an array of corresponding values for all fields. 1 and 2 correspond to each other. $match[1][0]=$match[2][0] 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/ / /)/ / /)

Link of this Article: Class SQL canonical seeking Teaching

Leave a Reply

Your email address will not be published. Required fields are marked *