我有一个包含2条信息的字符串(1.A Boolean / 2.Something(可以是数字,字母,特殊字符,并且可以是任何长度))。
2是用户输入。
例子:
(part 1)"true".(part 2)"321654987" => "true321654987"
也可能是
"false321654987" or "trueweiufv2345fewv"
我需要的是一种解析字符串的方法,首先检查1是否为true
(如果为false则不执行任何操作),如果true
我需要检查其后部分是否为大于0的正数(必须接受大于0的任何数字) 0甚至是小数,但不是bin或hex(嗯...可能是10,但意味着10而不是2))。
这是我尝试过的:
//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到string的内容。但是它不匹配十进制数。
我知道如何做一个正则表达式来解析小数,这不是问题。
问题是:
php中已经有一个函数可以匹配我需要的解析?
如果不是,你们中的任何人都知道一种更有效的解析方法吗?还是我应该将regEx的小数部分添加到我的正则表达式中?
我在想类似的东西:
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.
任何的想法??
非常感谢你的帮助!
用途substr
:文档
if(substr($value, 0, 4) == "true"){
$number_part = substr($value, 5);
if(((int) $number == $number) || ((float) $number == $number)){
//do something...
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句