如何在PHP中减去字符串中的数字?
问题描述:
There is a string variable containing number data , say $x = "OP/12/DIR";
. The position of the number data may change at any circumstance by user desire by modifying it inside the application , and the slash bar may be changed by any other character ; but the number data is mandatory. So how to extract the number data from the string ?
有一个包含数字数据的字符串变量,比如 $ x =“OP / 12 / DIR”; code>。 数字数据的位置可以在任何情况下根据用户的需要通过在应用程序内部进行修改而改变,并且斜杠可以由任何其他字符改变; 但数字数据是强制性的。 那么如何从字符串中提取数字数据? p>
div>
答
Replace everything that is NOT a number with an empty string.
$numbers = preg_replace('/[^0-9]*/','',$x);
答
Replace everithing that is not a number:
$numbers = preg_replace( '/[^\d\.]/', '', $input );
or if you will have decimal:
$numbers = preg_replace ( '#\D*?(\d+(\.\d+)?)\D*#', '$1', $input );