Php 解析包含数字的字符串

Php 解析包含数字的字符串,php,regex,string,parsing,Php,Regex,String,Parsing,我有一个包含2个信息的字符串(1.a Boolean/2.Something(可以是数字、字母、特殊字符,也可以是任意长度)) 2是用户输入 例如: (part 1)"true".(part 2)"321654987" => "true321654987" 也可能是 "false321654987" or "trueweiufv2345fewv" 我需要的是一种解析字符串的方法,首先检查1是否为true(如果为false,则不执行任何操作),如果为true,我需要检查to后面的部分是否

我有一个包含2个信息的字符串(1.a Boolean/2.Something(可以是数字、字母、特殊字符,也可以是任意长度))

2是用户输入

例如:

(part 1)"true".(part 2)"321654987" => "true321654987"
也可能是

"false321654987" or "trueweiufv2345fewv"
我需要的是一种解析字符串的方法,首先检查1是否为
true
(如果为false,则不执行任何操作),如果为
true
,我需要检查to后面的部分是否是大于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...
        }
    }
}
如您所见,我使用正则表达式将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.
有什么想法吗


非常感谢你的帮助

使用
substr


这应该做到这一点,并处理两种类型的值:

preg_match('/^(true|false)(.*)$/', $value, $matches);

$real_val = $matches[2];

if ($matches[1] == 'true') {
  ... true stuff ...
} else if ($matches[1] == 'false') {
  ... false stuff ...
} else { 
  ... file not found stuff ...
}
尝试一下:

else if(!preg_match('#^true([1-9][0-9]*(?:\.[0-9]*)?$#', $value))
看看:

检查所提供字符串(文本)中的所有字符是否都是数字。

要检查小数,可以使用
filter\u var

if (filter_var('123.45', FILTER_VALIDATE_FLOAT) !== false) {
    echo 'Number';
} 
您可以这样做:

case "txtFSPLongRuban":
    if (preg_match('~^true(?=.*[^0.])([0-9]+(?:\.[0-9]+)?)$~', $value, $match))
    {
        // do what you want with $match[1] that contains the not null number.  
    }
break;

前瞻
(?=.[^0.])
检查是否有某个字符不是
0

在本例中不执行任何操作时,为什么要检查字符串是否以false开头?你要做的唯一一件事就是检查它是否以“真”开头。很抱歉,我正计划在False上添加一条错误消息,但我认为目前这并不重要。那么数字部分呢?添加。在展示了substr的用法之后,我认为这是合乎逻辑的。我将把这个答案与@MarkB混合在一起,真的很喜欢演员阵容!感谢您的回答,添加了应该与浮点数匹配的筛选器_var()。
case "txtFSPLongRuban":
    if (preg_match('~^true(?=.*[^0.])([0-9]+(?:\.[0-9]+)?)$~', $value, $match))
    {
        // do what you want with $match[1] that contains the not null number.  
    }
break;