1

I am trying to convert PHP array to YAML. I want the the desired output to be without wordwrap but yaml_emit() break the lines into multiple lines. I want the string to remain in one line.

---
bill: William Henry Gates III is an American business magnate, software developer,
  investor, author, and philanthropist. He is the co-founder of Microsoft Corporation.
steve: Steven Paul Jobs was an American business magnate, industrial designer, investor,
  and media proprietor.
...

Desired output:

---
bill: William Henry Gates III is an American business magnate, software developer, investor, author, and philanthropist. He is the co-founder of Microsoft Corporation.
steve: Steven Paul Jobs was an American business magnate, industrial designer, investor, and media proprietor.
...

Does anyone know who to achieve that? Here's the code:

<?php
$array = array(
'bill' => 'William Henry Gates III is an American business magnate, software developer, investor, author, and philanthropist. He is the co-founder of Microsoft Corporation.',
'steve' => 'Steven Paul Jobs was an American business magnate, industrial designer, investor, and media proprietor.'
);
file_put_contents('bio.yaml', yaml_emit($array));
?>
  • 3
    Just to clarify: you are not viewing yaml though tool that auto-splits long lines? Like browser. – Justinas Jul 14 '21 at 11:01
  • @Justinas i am not using any tool, this is output from php. i am saving the output in `.yaml` file and this is how the data is saved. – маньяк Jul 14 '21 at 11:05
  • Because I do not see any file output, just `echo`. Also I can't run it, but do not think tool would break functionality – Justinas Jul 14 '21 at 11:23
  • It looks like `yaml_emit()` itself does not have option for that ([for example pythons yaml.dump() has parameter width](https://stackoverflow.com/questions/18514205/how-to-prevent-yaml-to-dump-long-line-without-new-line)). Just to be clear it's still valid YAML. – Hendrik Jul 14 '21 at 11:23
  • @Justinas I updated the code. to run `yaml_emit()`, you will need php yaml extension. – маньяк Jul 14 '21 at 11:30
  • 1
    @Hendrik it is a valid yaml, works fine with `yaml_parse()` but the parser my company is using for the `.yaml` files do not read it unless the whole string is in same line. – маньяк Jul 14 '21 at 11:34
  • 2
    As a rule of thumb, if a tool generates valid YAML and another tool can't parse that YAML then the right thing to fix is to the tool that can't deal with valid YAML (and not to hack the tool generating valid YAML to generate a specific sub-set of YAML) – Quentin Jul 14 '21 at 13:05

1 Answers1

2

php-yaml's yaml_emit() does not seem to have an option for controlling line length. If using third party libraries is an option then it seems that symfony/yaml behaves like you desire.

<?php
use Symfony\Component\Yaml\Yaml;

$array = array(
'bill' => 'William Henry Gates III is an American business magnate, software developer, investor, author, and philanthropist. He is the co-founder of Microsoft Corporation.',
'steve' => 'Steven Paul Jobs was an American business magnate, industrial designer, investor, and media proprietor.'
);

echo Yaml::dump($array);

Output:

bill: 'William Henry Gates III is an American business magnate, software developer, investor, author, and philanthropist. He is the co-founder of Microsoft Corporation.'
steve: 'Steven Paul Jobs was an American business magnate, industrial designer, investor, and media proprietor.'

https://symfony.com/doc/current/components/yaml.html (Installation)

https://packagist.org/packages/symfony/yaml

Hendrik
  • 756
  • 6
  • 16