【发布时间】:2013-11-29 15:23:46
【问题描述】:
我有一个包含 2 个信息的字符串(1.A Boolean/2.Something(可以是数字、字母、特殊字符,并且可以是任意长度)。
2 是用户输入。
示例:
(part 1)"true".(part 2)"321654987" => "true321654987"
也可以
"false321654987" or "trueweiufv2345fewv"
我需要的是一种解析字符串的方法,首先检查 1 是否为 true(如果其为 false,则不执行任何操作),如果是 true,我需要检查以下部分是否为正数大于 0(必须接受任何高于 0 的数字,即使是十进制但不是 bin 或十六进制(嗯......可能是 10 但它意味着十而不是二)。
这是我尝试过的:
//This part is'nt important it work as it should....
if(isset($_POST['validate']) && $_POST['validate'] == "divSystemePositionnement")
{
$array = json_decode($_POST['array'], true);
foreach($array as $key=>$value)
{
switch($key)
{
case "txtFSPLongRuban":
//This is the important stuff HERE.....
if(preg_match('#^false.*$#', $value))//If false do nothing
{}
else if(!preg_match('#^true[1-9][0-9]*$#', $value))//Check if true and if number higher than 0.
{
//Do stuff,
//Some more stuff
//Just a bit more stuff...
//Done! No more stuff to do.
}
break;
//Many more cases...
}
}
}
如您所见,我使用 regEx 将 trought 解析为字符串。但它与十进制数不匹配。
我知道如何用正则表达式解析小数,这不是问题。
问题是:
php 中是否已经有与我需要的解析匹配的函数?
如果没有,你们中是否有人知道一种更有效的解析方法,或者我应该将小数部分添加到我的正则表达式中吗?
我在想这样的事情:
test = str_split($value, "true")
if(isNumeric(test[1]) && test[1] > 0)
//problem is that isNumeric accepte hex and a cant have letter in there only straight out int or decimal number higher than 0.
有什么想法吗??
非常感谢您的帮助!
【问题讨论】:
-
在这种情况下什么都不做时,为什么要检查字符串是否以 false 开头?您唯一要做的就是检查它是否以“true”开头
-
对不起,我打算在 False 上添加一条错误消息,但我认为这暂时不重要。