我的 PHP 文件接收(通过 $_POST)字符串(带有常量前缀),例如:
(本例中的常量前缀 = 'astring';小数点前后的数字大小可能不同)
- astring1.1
- 字符串1.2 ..
- astring23.2
- astring23.6
如何获得小数点后面的值?我知道如何从常量前缀中提取总数字,但我需要使用小数点前后的数字,不知道如何提取这些数字。以某种方式使用 preg_match?
我的 PHP 文件接收(通过 $_POST)字符串(带有常量前缀),例如:
(本例中的常量前缀 = 'astring';小数点前后的数字大小可能不同)
如何获得小数点后面的值?我知道如何从常量前缀中提取总数字,但我需要使用小数点前后的数字,不知道如何提取这些数字。以某种方式使用 preg_match?
如果你想要小数点前的数字。尝试这个。
$number = 2.33;
echo floor($number);
试试explode
喜欢
$str = 'astring23.2';
$str_arr = explode('.',$str);
echo $str_arr[0]; // Before the Decimal point
echo $str_arr[1]; // After the Decimal point
list($before, $after) = explode(".", $string);
echo "$before is the value before the decimal point!";
echo "$after is the value after the decimal point!";
一种简单的方法(php 5.3+):
$str = 'astring23.2';
$pre = strstr($str, '.', true);
你想要类似的东西吗?
<?php
$string = "astring23.6";
$data = explode("astring", $string); // removed prefix string "astring" and get the decimal value
list($BeforeDot, $afterDot)=explode(".", $data[1]); //split the decimal value
echo "BeforeDot:".$BeforeDot." afterDot: ". $afterDot;
?>
如果您只想要数字,请像这样使用
$str = 'astring23.2';
$str_arr = explode('.',$str);
echo preg_replace("/[a-z]/i","",$str_arr[0]); // Before the Decimal point
echo $str_arr[1]; // After decimal point