分割数字并在点后保留一个数字

I have following string: $string = "10x1.12A" and I wanted result to be: 10x1.1 . I have different combinations at the end but I just wanted to get one number after the dot and remove everything after that.

评论
  • 尔如此冷淡
    尔如此冷淡 回复

    I might suggest using preg_match_all with the regex pattern ^.*?\.\d:

    $string = "10x1.12A";
    preg_match_all ("/^.*?\.\d/", $string, $matches);
    echo $matches[0][0];
    

    打印:

    10x1.1
    

    There is also possibly a preg_replace way of doing this:

    $string = "10x1.12A";
    $output = preg_replace("/(?<=\.\d).*$/", "", $string);
    echo $output;
    

    这种方法去除了点号后出现的所有内容。请注意,我在这里假设只有一个点。