简体   繁体   English

PHP 预匹配 - 具有浮点值的正则表达式

[英]PHP preg match - regular expression with float value

have this string:有这个字符串:

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

I need to get out 30.5 and 120.8 with a regular expression.. any help??我需要用正则表达式输出 30.5 和 120.8 .. 有什么帮助吗?? 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 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?也适用于 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]并使用$result[1]$result[2]

The following should do the trick: /^(\d+)x(\d+)/以下应该可以解决问题: /^(\d+)x(\d+)/

Running code运行代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM