开发者

PHP preg match - regular expression with float value

have this string:

$var = "30.5x120.8 (test desc here)";

I need to g开发者_如何学Cet out 30.5 and 120.8 with a regular expression.. any help?? Thx


preg_match_all('~\d+(?:\.\d+)?~', $string, $matches);
var_dump($matches[0]);


$var = "30x120 (test desc here)";
 
preg_match_all('/^(\d+)x(\d+)/', $var, $matches);
 
var_dump($matches)

Ideone.

Output

array(3) {
  [0]=>
  array(1) {
    [0]=>
    string(6) "30x120"
  }
  [1]=>
  array(1) {
    [0]=>
    string(2) "30"
  }
  [2]=>
  array(1) {
    [0]=>
    string(3) "120"
  }
}

Update

also work for 17.5x17.5 ?

Here is one which will...

/^(\d+(?:\.\d+)?)x(\d+(?:\.\d+)?)/

Ideone.


preg_match('/^(\d+)x(\d+)/', '30x120 (test desc here)', $result);

and use $result[1] and $result[2]


The following should do the trick: /^(\d+)x(\d+)/

Running code

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜