简体   繁体   中英

Regular expression to get any numbers not only integers

I have this string and need to get the number (integer or double) out of it.

String 1 GMT +2

String 2 - GMT +2.5

I have used this to get the integer out of String 1 and need to know how to write a generic one to get 2 or 2.5

below is what i have written.

return preg_replace('/\D/', '', $str); 

but it doesnt return 2.5 but takes it as 25 instead of 2.5

假设您的输入始终以数字结尾,则可以使用稍微修改的代码:

preg_replace('/^\D*/', '', $str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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