Practical YAML subset parser and emitter for HolyPHP with zero dependencies. Supports nested mappings, block sequences, inline arrays and mappings, quoted and unquoted scalars, booleans, numbers, nulls, comments, and clear line-numbered syntax and indentation errors.
Comments:# ... on full lines or inline after whitespace
Document markers: optional leading --- and trailing ...
Parameters:
$input (string): YAML text to parse.
Returns: Parsed HolyPHP value (array, string, int, float, bool, or null).
---
yaml_stringify($value, int $indent = 2): string
Serializes a HolyPHP value (array, string, int, float, bool, or null) into a formatted YAML string. Automatically quotes strings that contain special characters or look like booleans, numbers, or null.
Parameters:
$value: Value to serialize.
$indent (int, optional, default 2): Number of spaces per indentation level. Must be greater than 0.
Returns: YAML-formatted string.
Error Handling
Invalid YAML or invalid options throw an Exception prefixed with yaml: and a line number when parsing:
yaml: line 2: tabs are not allowed for indentation
yaml: line 1: unterminated double-quoted string
yaml: line 1: unterminated single-quoted string
yaml: line 1: unterminated inline array
yaml: line 2: unexpected indentation in mapping (expected 0, got 2)
yaml: line 1: trailing comma in inline collection
yaml: indent must be a positive integer
Examples
01Quick start
Bundled with the compiler — import "yaml"; works offline in any file, or add it to a project with hphp install yaml.