我有两个这样的字符串:
$a = "John, Male , Central Java";
$b = "name = John and gender= Male";
我希望这些字符串成为:
$a = "'John','Male','Central Java'";
$b = "username='John' and gender='Male'";
使用preg_replace可以使用哪种模式和替换来实现此目的?
我想创建一个这样的函数:
function select($what, $from, $filter) {
$query = "SELECT $what FROM $from WHERE $filter";
// Performing mysql query.
}
$result = select("*", "user", "username = John and gender = Male");
$ quer输出:SELECT * FROM user WHERE username = John and gender = Male
但输出的mysql语法无效。所以我希望输出成为:
SELECT * FROM user WHERE username='John' and gender='Male'
我也想删除符号之间的空格。
解决方案:
我尝试了一些模式和替换,最后我找到了解决方案。我创建了用于格式化查询字符串的函数。我还使用&&
作为AND
和||
作为OR
来更改字符串设置。因此,即使值字符串包含'和'或'或',它也不会受到preg_replace的影响。
// Preg replace sample: -> string = string space && string = space
function format_query($qry_string) {
// Remove spaces before and after '='.
$qry_string = preg_replace("/(\s+\=)/", "=", $qry_string);
// string= string space && string= space
$qry_string = preg_replace("/(\=\s+)/", "=", $qry_string);
// string=string space && string=space
// Add single quote between '=' and word than contain space after the word.
$qry_string = preg_replace("/\=(\w+\s+)/", "='$1", $qry_string);
// string='string space && string=space
// Add single quote in first and end of words that start after '=' and not contain space after the word.
$qry_string = preg_replace("/\=(\w+)/", "='$1'", $qry_string);
// string='string space && string='space'
// Add single quote in end of words before of keyword '&&' and '||'.
$qry_string = preg_replace("/(\w+)\s+\&\&/", "$1' &&", $qry_string);
// string='string space' && string='space'
$qry_string = preg_replace("/(\w+)\s+\|\|/", "$1' ||", $qry_string);
// Replate keyword '&&' and '||' to 'and' and 'or'
$qry_string = str_replace("&&", "AND", $qry_string);
$qry_string = str_replace("||", "OR", $qry_string);
return $qry_string;
}
$string = "realname = Nanang El Agung || username = sarjono && password = 123456";
echo format_query($string);
输出:realname='Nanang El Agung' OR username='sarjono' AND password='123456'
此功能适用于上述$b
。
答案 0 :(得分:2)
这就是这样,对于第一个例子,测试它。
<?php
$a = "John, Male , Central Java";
foreach ((explode(",", $a)) as $one) {
echo '\''.$one.'\',';
}
输出
'John',' Male ',' Central Java',
关于Mysql查询,这样的事情是可行的。
<?php
$a = "John, Male , Central Java";
foreach ((explode(",", $a)) as $one) {
function select($one) { // will run the three statements, relatively..
echo '\''.$one.'\',';
}
第二个
$b = "name='John' and gender='Male'";
$b = str_replace('name', 'username', $b);
foreach((explode("=",$b[1]))as $one){
echo "$b";
}
答案 1 :(得分:2)
$a = "John, Male , Central Java";
$b = "name = John and gender= Male";
$a = preg_replace('/(\w+)((\s?\w+)+)/', '\'$1$2\'', $a);
// 'John', 'Male' , 'Central Java'
$b = preg_replace('/name\s?=\s?([\w\s]+) and gender\s?=\s?([\w\s]+)/', 'username = \'$1\' AND gender = \'$2\'', $b);
// username = 'John' AND gender = 'Male'
\ w =&gt; [A-ZA-Z0-9 _]
\ s =&gt;空间
+ =&gt; 1个或多个字符
? =&gt; 0或1个字符
答案 2 :(得分:1)
第二部分是
function select($what, $from, $filter) {
$query = mysql_fetch_array(mysql_query("SELECT {$what} FROM {$from} WHERE {$filter}")); extract($query);
return $what;
}
答案 3 :(得分:0)
不要这样做。您正在尝试使用正则表达式构造SQL查询。您的代码(包括所有三个当前答案)将在第一个走进门的O'Connor
中断。如果你要解决这个问题并以某种方式将你的代码放到网上,那么你将受到第一个黑客的怜悯,他们将尝试一系列众所周知的漏洞,直到找到你没想到的角落案例。
有关完整的详细信息以及构建SQL语句的正确方法,请参阅this question和this answer。