【问题标题】:Parsing a string that contain Numbers PHP解析包含数字 PHP 的字符串
【发布时间】: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 上添加一条错误消息,但我认为这暂时不重要。

标签: php regex string parsing


【解决方案1】:

使用substrdocumentation

if(substr($value, 0, 4) == "true"){
   $number_part = substr($value, 5);
   if(((int) $number == $number) || ((float) $number == $number)){
      //do something...
   }
}

【讨论】:

  • 数字部分呢?
  • 已添加。在展示了 substr 的使用之后,我认为这是合乎逻辑的。
  • 我会将这个答案与@MarkB 混合在一起 真的很喜欢演员阵容!谢谢
【解决方案2】:

你可以这样做:

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. 的字符

【讨论】:

    【解决方案3】:

    这应该可以解决问题,并处理两种类型的值:

    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 ...
    }
    

    【讨论】:

      【解决方案4】:

      试一试:

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

      【讨论】:

        【解决方案5】:

        看看ctype_digit:

        Checks if all of the characters in the provided string, text, are numerical.

        要检查小数,您可以使用filter_var

        if (filter_var('123.45', FILTER_VALIDATE_FLOAT) !== false) {
            echo 'Number';
        } 
        

        【讨论】:

        • 已编辑答案,添加了也应匹配浮点数的 filter_var()。
        猜你喜欢
        • 2011-12-19
        • 1970-01-01
        • 2012-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-14
        • 2020-04-28
        • 2012-11-09
        相关资源
        最近更新 更多