PHP sscanf() 함수
예시
문자열 구문 분석:
<?php
$str = "age:30 weight:60kg";
sscanf($str,"age:%d weight:%dkg",$age,$weight);
// show types and values
var_dump($age,$weight);
?>
sscanf() 함수는 지정된 형식에 따라 문자열의 입력을 구문 분석합니다. sscanf() 함수는 형식 문자열을 기반으로 문자열을 변수로 구문 분석합니다.
이 함수에 두 개의 매개변수만 전달되면 데이터는 배열로 반환됩니다. 그렇지 않고 선택적 매개변수가 전달되면 구문 분석된 데이터가 해당 매개변수에 저장됩니다. 포함할 변수보다 지정자가 많으면 오류가 발생합니다. 그러나 변수보다 지정자가 적은 경우 추가 변수에는 NULL이 포함됩니다.
관련 기능:
통사론
sscanf(string,format,arg1,arg2,arg++)
매개변수 값
Parameter | Description |
---|---|
string | Required. Specifies the string to read |
format | Required. Specifies the format to use. Possible format values:
Additional format values. These are placed between the % and the letter (example %.2f):
Note: If multiple additional format values are used, they must be in the same order as above. |
arg1 | Optional. The first variable to store data in |
arg2 | Optional. The second variable to store data in |
arg++ | Optional. The third, fourth, and so on, to store data in |
기술적 세부 사항
반환 값: | 이 함수에 두 개의 매개변수만 전달되면 데이터는 배열로 반환됩니다. 그렇지 않고 선택적 매개변수가 전달되면 구문 분석된 데이터가 해당 매개변수에 저장됩니다. 포함할 변수보다 지정자가 많으면 오류가 발생합니다. 그러나 변수보다 지정자가 적은 경우 추가 변수에는 NULL이 포함됩니다. |
---|---|
PHP 버전: | 4.0.1+ |
더 많은 예
예시
형식 값 %s, %d 및 %c 사용:
<?php
$str = "If you divide 4 by 2 you'll get 2";
$format = sscanf($str,"%s %s %s %d %s %d %s %s %c");
print_r($format);
?>
❮ PHP 문자열 참조