1

我有两个这样的字符串:

$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");

$查询输出:SELECT * FROM user WHERE username = John and gender = Male

但输出不是有效的 mysql 语法。所以我希望输出变成:

SELECT * FROM user WHERE username='John' and gender='Male'

我还想删除符号之间的空格。

解决方案:

我尝试了一些模式和替换,最后我找到了解决方案。我已经创建了格式化查询字符串的函数。我还使用&&asAND||as更改了字符串设置OR。因此,即使值字符串包含'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上述情况。

4

4 回答 4

2

使用preg_replace()

$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 => [a-zA-Z0-9_]

\s => 空间

+ => 1 个或多个字符

? => 0 或 1 个字符

于 2013-03-31T19:00:10.640 回答
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";
}
于 2013-03-31T18:03:25.497 回答
1

第二部分是

function select($what, $from, $filter) {
$query = mysql_fetch_array(mysql_query("SELECT {$what} FROM {$from} WHERE {$filter}")); extract($query);
return $what;
}
于 2013-03-31T18:07:37.723 回答
0

不要这样做。您正在尝试使用正则表达式构建 SQL 查询。您的代码(包括所有三个当前答案)将在第一个O'Connor走进门时中断。如果您要解决此问题并以某种方式将您的代码放到网络上,那么您将受第一个黑客的摆布,他们会尝试一系列众所周知的漏洞,直到他们找到您没有想到的极端情况。

有关完整的详细信息和构建 SQL 语句的正确方法,请参阅此问题此答案。

于 2013-04-03T14:20:24.210 回答