PHP提取运算符和String中的值

时间:2015-09-07 10:49:38

标签: php

在我的代码中,我需要同时获取运算符和值来进行计算。我的代码是,

  

$ demension =" 3.5x2.3 => 4.8x8.9"

    public function searchItems($dimension)
    {
    $out= preg_split('/[x-]/', $dimension);
    $i=0;
    foreach ($out as $key) {
        preg_match('/(!=|=|<=|<|>=|>)/',$key,$matches);
        if(!empty($matches))
        {
            $result[$i]=$matches;
        }else
        {
            $result[$i]=$key;
        }

        $i++;
    }


    return $result;

}

我需要分别获得3.5,2.3,=&gt;,4.8,8.9 任何人都可以向我展示正确的道路。

1 个答案:

答案 0 :(得分:2)

在这里。使用正则表达式获取值和运算符。

function getParts($string)
{
    $regexp = "/(\d+?\.\d+?)|([<>=]+)/";

    $parts = [];

    if (preg_match_all($regexp, $string, $matches)) {
        $parts = $matches[0];
    }

    return $parts;
}

$dimension = "3.5x2.3=>4.8x8.9"
print_r(getParts($dimension));