2

我的 PHP 文件接收(通过 $_POST)字符串(带有常量前缀),例如:

(本例中的常量前缀 = 'astring';小数点前后的数字大小可能不同)

  • astring1.1
  • 字符串1.2 ..
  • astring23.2
  • astring23.6

如何获得小数点后面的值?我知道如何从常量前缀中提取总数字,但我需要使用小数点前后的数字,不知道如何提取这些数字。以某种方式使用 preg_match?

4

6 回答 6

4

如果你想要小数点前的数字。尝试这个。

$number = 2.33;
echo floor($number);
于 2016-08-11T07:03:28.673 回答
3

试试explode喜欢

$str = 'astring23.2'; 
$str_arr = explode('.',$str);
echo $str_arr[0];  // Before the Decimal point
echo $str_arr[1];  // After the Decimal point
于 2013-11-06T11:21:17.373 回答
3
list($before, $after) = explode(".", $string);

echo "$before is the value before the decimal point!";
echo "$after is the value after the decimal point!";
于 2013-11-06T11:21:46.953 回答
3

一种简单的方法(php 5.3+):

$str = 'astring23.2';
$pre = strstr($str, '.', true);
于 2013-11-06T11:22:00.230 回答
0

你想要类似的东西吗?

    <?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;

    ?>
于 2013-11-06T11:38:06.880 回答
0

如果您只想要数字,请像这样使用

$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
于 2013-11-06T11:45:51.850 回答