Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 需要字符串解析方面的帮助吗?_Php_Parsing_String - Fatal编程技术网

Php 需要字符串解析方面的帮助吗?

Php 需要字符串解析方面的帮助吗?,php,parsing,string,Php,Parsing,String,我正在根据用户输入创建SQL查询: // user input $amount = '20'; // query in zend $select->where('amount = ?', $amount ); 现在,根据新的要求,用户还可以输入以下格式: $amount = '20'; $amount = '<20'; $amount = '<=20'; $amount = '>20'; $amount = '>=20'; $amount = '=20';

我正在根据用户输入创建SQL查询:

// user input
$amount = '20';

// query in zend 
$select->where('amount = ?', $amount );
现在,根据新的要求,用户还可以输入以下格式:

$amount = '20';
$amount = '<20';
$amount = '<=20';
$amount = '>20';
$amount = '>=20';
$amount = '=20';
我想你了解这个新功能。那个么,我如何解析$amount变量来适应上面的查询呢?我必须将数字和符号与$amount变量分开,以便在适当的位置使用它们。如果有错误的符号$amount='$%20';那么它应该被认为是相等的。p> 我应该采取什么方法来解决这个问题


谢谢

如果用户可以使用值指定运算符,您必须小心。尝试:

$input = '<=20';
$allowedOperators = array('<', '<=', '>', '>=', '=');

if ( in_array(substr($input,0,2), $allowedOperators) ) {
    $operator = substr($input,0,2);
} else if ( in_array(substr($input,0,1), $allowedOperators) ) {
    $operator = substr($input,0,1);
} else {
    $operator = '=';
}

$amount = (int) substr($input, strlen($operator));

$select->where('amount ' . $operator . ' ?', $amount );

如果用户可以使用值指定运算符,则必须小心。尝试:

$input = '<=20';
$allowedOperators = array('<', '<=', '>', '>=', '=');

if ( in_array(substr($input,0,2), $allowedOperators) ) {
    $operator = substr($input,0,2);
} else if ( in_array(substr($input,0,1), $allowedOperators) ) {
    $operator = substr($input,0,1);
} else {
    $operator = '=';
}

$amount = (int) substr($input, strlen($operator));

$select->where('amount ' . $operator . ' ?', $amount );

您可以使用以下正则表达式来分隔它们:

preg_match('/(<|>|=|<=|>=)([0-9]+)/', $amount, $matches);
$sign=$matches[1];
$number=$matches[2];
更新:我已经测试过了,发现它不能正确处理错误的信号。我提出了以下代码,它似乎工作得很好:

$amount=">=20";
preg_match('/([^0-9]*)([0-9]+)/', $amount, $matches);
$sign=preg_match('/^(>|<|=|<=|>=)$/', $matches[1]) ? $matches[1] : '=';
$number=$matches[2];

如果给定的字符串为>=20,则符号为>=且数字为“20”;如果给定的字符串为“%$20”,则符号为=且数字为20。

可以使用以下正则表达式将它们分开:

preg_match('/(<|>|=|<=|>=)([0-9]+)/', $amount, $matches);
$sign=$matches[1];
$number=$matches[2];
更新:我已经测试过了,发现它不能正确处理错误的信号。我提出了以下代码,它似乎工作得很好:

$amount=">=20";
preg_match('/([^0-9]*)([0-9]+)/', $amount, $matches);
$sign=preg_match('/^(>|<|=|<=|>=)$/', $matches[1]) ? $matches[1] : '=';
$number=$matches[2];


如果给定的字符串>=20,则符号>=且数字为“20”;如果给定的字符串为“%$20”,则符号为=且数字为20。

首先,假设所有输入均正确,并重写$select->where'amount=?”,$amount;要$select->where'amount?,$amount;,它能解决你的问题吗?如果是,那么您可以使用一个小的正则表达式(例如“[0-9]*”)从输入中找到整数值,使用您拥有的int值去掉输入。但用户可以输入错误的符号$amount='$%20';如我的问题所述,用户可以输入任意数量的字符吗?我可以输入一个完整的副本吗?在输入中粘贴一个完整的网页?它将始终像SignsMembersFirst一样,假设所有输入都正确,并重写$select->where'amount=?',$amount;要$select->where'amount?,$amount;,它能解决你的问题吗?如果是,那么您可以使用一个小的正则表达式(例如“[0-9]*”)从输入中找到整数值,使用您拥有的int值去掉输入。但用户可以输入错误的符号$amount='$%20';如我的问题所述,用户可以输入任意数量的字符吗?我可以输入完整的副本吗?在输入中粘贴完整的网页吗?它将始终像SignsMembers它会处理%^$%^20吗?或者,/[]?=?[0-9]+/$matches[0]不包含运算符,而是包含完整的匹配项。捕获的部分分别位于$matches[1]和$matches[2]中。此外,这不包括未指定运算符的情况。@Stefan是的,这是一个输入错误。在测试我的解决方案后更新了我的答案。现在它应该可以正常工作了。它会处理%^$%^20吗?或者,/[]?=?[0-9]+/$matches[0]不包含运算符,而是包含完整的匹配项。捕获的部分分别位于$matches[1]和$matches[2]中。此外,这不包括未指定运算符的情况。@Stefan是的,这是一个输入错误。在测试我的解决方案后更新了我的答案。现在它应该很好用了。比我的溶液干净多了。为什么在数组中存在时使用第二个正则表达式:。您在哪里设置$parts[2]的值。在第三行中,没有$parts[2]。是打字错误吗?$parts在preg_match调用后包含3个键。0是不需要的全部匹配,1是从第一个括号对捕获的匹配,2是从第二个括号对捕获的匹配。@Stefan Gehrig:工作正常。谢谢。@Stefan Gehrig:有一个小问题。当存在浮点数时,它不起作用。例如:>45.15或=10.00。这没有包括在我的问题中。对不起。你有快速的解决办法吗。谢谢+1比我的更干净的解决方案。为什么在数组中存在时使用第二个正则表达式:。您在哪里设置$parts[2]的值。在第三行中,没有$parts[2]。是打字错误吗?$parts在preg_match调用后包含3个键。0是不需要的全部匹配,1是从第一个括号对捕获的匹配,2是从第二个括号对捕获的匹配。@Stefan Gehrig:工作正常。谢谢。@Stefan Gehrig:有一个小问题。当存在浮点数时,它不起作用。例如:>45.15或=10.00。这没有包括在我的问题中。对不起。你有快速的解决办法吗。谢谢,谢谢。这个解决方案有点长,但很灵活。我可以在以后的$allowedOperators数组中添加更多选项。我也将尝试此方法。存在一些问题:我尝试了$input=^&21,然后输出为servicerecord.TotalFees=0。应该是servicerecord.TotalFees=21谢谢。这个解决方案有点长,但很灵活。我可以在以后的$allowedOperators数组中添加更多选项。我也将尝试此操作。存在一些问题:我尝试了$input=^&21,然后输出为servicerecord.TotalFees=0 . 它应该是servicerecord.TotalFees=21