note 129219 added to function.yaml-emit

From: Date: Sun, 04 Feb 2024 01:37:26 +0000
Subject: note 129219 added to function.yaml-emit
Groups: php.notes 
Request: Send a blank email to php-notes+get-221173@lists.php.net to get a copy of this message
The list format doesn't have an indent below on the list name, that make me can't fold the list content by list name when using sublime text. I write a function to add two spaces below the list name, make the fold can work. before: --- k: v list: - k: v - k1: - 1 - 2 - 3 k1: - 1 - 2 - 3 ... after: --- k: v list: - k: v - k1: - 1 - 2 - 3 k1: - 1 - 2 - 3 ... <?php function format_yaml_indent($yaml) { $result = []; $list = explode("\n", $yaml); $padding = ' '; $padding_times = 0; $indent_count = 0; $pre_indent_count = 0; foreach ($list as $index => $row) { if (preg_match('/^\s*-? /', $row, $math)) { if (strpos($row, '-') !== false) { $indent_count = strlen($math[0]); if ($indent_count > $pre_indent_count) { $padding_times += 1; } else if ($indent_count < $pre_indent_count) { $padding_times -= 1; } } } else { $indent_count = 0; $padding_times = 0; } $pre_indent_count = $indent_count; if ($padding_times > 0) { $row = str_repeat($padding, $padding_times).$row; } $result[] = $row; } return join("\n", $result); } $array = [ 'k' => 'v', 'list' => [ ['k' => 'v'], ['k1' => [ 1,2,3 ]], ], 'k1' => [ 1,2,3 ] ]; $yaml = yaml_emit($array); echo "before:\n"; echo $yaml."\n"; echo "after:\n"; echo format_yaml_indent($yaml); ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.157.134 (proxied: 43.198.16.69) ---- Manual Page -- https://php.net/manual/en/function.yaml-emit.php Edit -- https://main.php.net/note/edit/129219 Del: integrated -- https://main.php.net/note/delete/129219/integrated Del: useless -- https://main.php.net/note/delete/129219/useless Del: bad code -- https://main.php.net/note/delete/129219/bad+code Del: spam -- https://main.php.net/note/delete/129219/spam Del: non-english -- https://main.php.net/note/delete/129219/non-english Del: in docs -- https://main.php.net/note/delete/129219/in+docs Del: other reasons-- https://main.php.net/note/delete/129219 Reject -- https://main.php.net/note/reject/129219 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221173) next »