0

我正在尝试从字符串示例中提取一些度量大小:

例子:

$s1=" foobar 10mm bar"              // 10mm
$s2=" foobar 10-300ml barbr"        // 10-300ml
$s3=" foobar 25 - 70 cm foo"        // 25-70cmm

我该怎么做?

我试过这样:

$sizes_volume_distance_markers = array('mm','ml','l','cm');
static  $pattern_final_voldist;
    if(!$pattern_final_voldist)
    {
        $pattern_final_voldist =   implode("|",self::$sizes_volume_distance_markers);
    }

if(preg_match_all("/([\d.-]{1,4}[\s.-]{0,2}($pattern_final_voldist) {1,2})+/i",$str,$vvalues))
       {
              var_dump($vvalues);
       }
4

1 回答 1

1

它应该可以工作,在最后一个括号中添加其他后缀:

/(\d+\s+?\-\s+?)?(\d+)\s+?(mm|ml|cm)/gi

用法:

$source=" foobar 10mm bar   foobar 10-300ml barbr  foobar 25 - 70 cm foo" 
preg_match_all("/(\d+\s+?\-\s+?)?(\d+)\s+?(mm|ml|cm)/i",$source, $results);

如果您需要匹配浮点数,请改用:

/(\d+(\.\d+)?\s+?\-\s+?)?(\d+(\.\d+)?)\s+?(mm|ml|cm)/gi
于 2012-07-27T09:18:02.320 回答