문제 설명
숫자를 나누고 점 뒤에 하나의 숫자를 유지하십시오. (Split the number and keep one number after the dot)
다음 문자열이 있습니다: $string = "10x1.12A"
그리고 결과는 10x1.1
입니다. 끝에 다른 조합이 있지만 점 뒤에 하나의 숫자를 가져오고 그 뒤에 모든 것을 제거하고 싶었습니다.
참조 솔루션
방법 1:
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];
This prints:
10x1.1
There is also possibly a preg_replace
way of doing this:
$string = "10x1.12A";
$output = preg_replace("/(?<=\.\d).*$/", "", $string);
echo $output;
This approach strips off everything which appears after the dot‑number. Note that I assume here that there would be only one dot.
방법 2:
$string = "10x1.12A" ;
$dotpos = strpos($string, '.'); // find the position of the first dot in the string
$result = substr($string, 0, $dotpos+2); // take the string from the start to the digit after the first dot
echo $result // 10x1.1
(by Akaash K、Tim Biegeleisen、Joffrey Schmitz)