PHP 8.1.28 Released!

yaml_parse

(PECL yaml >= 0.4.0)

yaml_parseYAML ストリームをパースする

説明

yaml_parse(
    string $input,
    int $pos = 0,
    int &$ndocs = ?,
    array $callbacks = null
): mixed

YAML ドキュメントストリーム全体あるいはその一部を、PHP の変数に変換します。

パラメータ

input

YAML ドキュメントストリームとしてパースする文字列。

pos

ストリームから取り出すドキュメント (-1 はすべてのドキュメント、0 は最初のドキュメント、...)。

ndocs

ndocs を渡すと、 ストリーム内で見つかったドキュメントの数がそこに格納されます。

callbacks

YAML ノードのコンテンツハンドラ。 YAML タグ => callable 形式の連想配列となります。 詳細はパースコールバック を参照ください。

戻り値

適切な PHP の型に変換した結果を返します。 失敗した場合に false を返します。 pos-1 の場合は配列を返します。 配列の各要素が、ストリーム内で見つかった個々のドキュメントとなります。

例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."
}

注意

警告

!php/object タグを使ったノードの unserialize() を有効にしている場合に、 ユーザーからの信頼できない入力を yaml_parse() で処理するのは危険です。 この挙動を無効にするには、ini 設定の yaml.decode_php を利用します。

参考

add a note

User Contributed Notes 3 notes

up
6
Zalmoksis
5 years ago
It seams the YAML standard version here is the 1.1 and not the (1.2 released in 2009), so all of the following values: y, Y, yes, Yes, YES, n, N, no, No, NO, true, True, TRUE, false, False, FALSE, on, On, ON, off, Off, OFF are confusingly at times interpreted as booleans.
up
6
alpacagm at gmail dot com
12 years ago
Simple usage example of interpreting custom tags:
<?php
$data
=
"#yaml
---
data: !mytag bar test
...
"
;

function
callback($value){
return
str_replace('bar','baz',$value);
}
$cnt;
$array=yaml_parse($data,0,$cnt,array('!mytag'=>'callback'));

print_r($array['data']);
?>
output:
Array
(
[data] => baz test
)
note, there is a related bug in version 1.0.1 (see tracker)
up
1
laura dot dean at mooneleaf dot com
7 years ago
When entering the string through a form, I had to first use utf8_decode on the input before using yaml_parse or else I got a cryptic error message --

Warning: yaml_parse(): reading error encountered during parsing: control characters are not allowed (line 1, column 1)

-- even though the special character was buried deep in the file.
To Top