Возможно, где-то есть косяки. Я особо не тестил.
protected function generatePostText($content)
{
$doc = new \DOMDocument('1.0');
foreach ($content['content'] as $node) {
$domElement = $this->nodeToDom($node, $doc);
if ($domElement !== null) {
$doc->appendChild($domElement);
}
}
return html_entity_decode($doc->saveHTML());
}
protected function nodeToDom($node, $doc)
{
$elementNode = null;
if (is_string($node)) {
return $doc->createTextNode($node);
}
if (isset($node['tag'])) {
$elementNode = $doc->createElement($node['tag']);
if (isset($node['attrs'])) {
foreach ($node['attrs'] as $attrName => $attrValue) {
if ($attrName === 'src' && $node['tag'] === 'img') {
if ($attrValue[0] === '/') {
$attrValue = "http://telegra.ph{$attrValue}";
}
}
$elementNode->setAttribute($attrName, $attrValue);
}
}
}
if (isset($node['children'])) {
foreach ($node['children'] as $child) {
$childNode = $this->nodeToDom($child, $doc);
if ($childNode !== null) {
$elementNode->appendChild($childNode);
}
}
}
return $elementNode;
}