0

I'm trying to extract some metric sizes from strings example:

Example:

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

How do I go about it ?

I've tried this so:

$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

It should works, add other suffixes in last parentheses:

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

Usage:

$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);

If you need to match floats use this instead:

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