YAML 데이터 직렬화 yaml_parse
(PECL yaml >= 0.4.0)
yaml_parse — Parse a YAML stream
설명
yaml_parse( string $input, int $pos = 0, int &$ndocs = ?, array $callbacks = null ): mixed
YAML 문서 스트림의 전체 또는 일부를 PHP 변수로 변환합니다.
매개변수
input
- YAML 문서 스트림으로 구문 분석할 문자열입니다.
pos
- 스트림에서 추출할 문서(모든 문서의 경우 -1, 첫 번째 문서의 경우 0, ...).
ndocs
ndocs
가 제공되면 스트림에서 찾은 문서 수로 채워집니다.callbacks
- YAML 노드를 내보내는 콘텐츠 핸들러. 클래스 이름의 연관 배열 => callable 매핑. 자세한 내용은 emit callbacks을 참조하세요.
반환 값
적절한 PHP 유형으로 input
에 인코딩된 값을 반환하거나 실패 시 false
를 반환합니다. pos
가 -1이면 스트림에서 찾은 각 문서에 대해 하나의 항목과 함께 배열이 반환됩니다.
Examples
예제 #1 yaml_parse() 예제
<?php
$yaml = <<<EOD
---
invoice: 34843
date: "2001-01-23"
bill-to: &id001
given: Chris
family: Dumars
address:
lines: |-
458 Walkman Dr.
Suite #292
city: Royal Oak
state: MI
postal: 48046
ship-to: *id001
product:
- sku: BL394D
quantity: 4
description: Basketball
price: 450
- sku: BL4438H
quantity: 1
description: Super Hoop
price: 2392
tax: 251.420000
total: 4443.520000
comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.
...
EOD;
$parsed = yaml_parse($yaml);
var_dump($parsed);
?>
위의 예는 다음과 유사한 결과를 출력합니다.
array(8) { ["invoice"]=> int(34843) ["date"]=> string(10) "2001-01-23" ["bill-to"]=> &array(3) { ["given"]=> string(5) "Chris" ["family"]=> string(6) "Dumars" ["address"]=> array(4) { ["lines"]=> string(34) "458 Walkman Dr. Suite #292" ["city"]=> string(9) "Royal Oak" ["state"]=> string(2) "MI" ["postal"]=> int(48046) } } ["ship-to"]=> &array(3) { ["given"]=> string(5) "Chris" ["family"]=> string(6) "Dumars" ["address"]=> array(4) { ["lines"]=> string(34) "458 Walkman Dr. Suite #292" ["city"]=> string(9) "Royal Oak" ["state"]=> string(2) "MI" ["postal"]=> int(48046) } } ["product"]=> array(2) { [0]=> array(4) { ["sku"]=> string(6) "BL394D" ["quantity"]=> int(4) ["description"]=> string(10) "Basketball" ["price"]=> int(450) } [1]=> array(4) { ["sku"]=> string(7) "BL4438H" ["quantity"]=> int(1) ["description"]=> string(10) "Super Hoop" ["price"]=> int(2392) } } ["tax"]=> float(251.42) ["total"]=> float(4443.52) ["comments"]=> string(68) "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338." }
메모
경고 yaml_parse()로 신뢰할 수 없는 사용자 입력을 처리하는 것은 !php/object
태그를 사용하는 노드에 대해 unserialize() 사용이 활성화된 경우 위험합니다. 이 동작은 yaml.decode_php
ini 설정을 사용하여 비활성화할 수 있습니다.
기타
- yaml_parse_file() - 파일에서 YAML 스트림 구문 분석
- yaml_parse_url() - URL에서 Yaml 스트림 구문 분석
- yaml_emit() - 값의 YAML 표현을 반환합니다.