Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有一个包含数字数据的字符串变量,比如说$x = "OP/12/DIR";。数字数据的位置可以在任何情况下根据用户的需要在应用程序中修改,斜线可以被任何其他字符改变;但数字数据是强制性的。那么如何从字符串中提取数字数据呢?
$x = "OP/12/DIR";
用空字符串替换不是数字的所有内容。
$numbers = preg_replace('/[^0-9]*/','',$x);
替换所有不是数字的内容:
$numbers = preg_replace( '/[^\d\.]/', '', $input );
或者如果你有小数:
$numbers = preg_replace ( '#\D*?(\d+(\.\d+)?)\D*#', '$1', $input );