function toHtml(array $content) {
$html = '';
foreach ($content as $row) {
if (is_string($row)) {
$html .= $row;
continue;
}
$attrs = '';
if (isset($row['attrs'])) {
foreach ($row['attrs'] as $name => $value) {
if ($row['tag'] === 'img') {
$value = '//telegra.ph' . $value;
}
$attrs .= sprintf(' %s="%s"', $name, $value);
}
}
$children = '';
if (isset($row['children'])) {
$children .= toHtml($row['children']);
}
$html .= sprintf('<%s%s>%s</%s>', $row['tag'], $attrs, $children, $row['tag']);
}
return $html;
}
$content = json_decode(
file_get_contents('https://api.telegra.ph/getPage/Vozmozhnosti-Telegram-03-30?return_content=true'),
true
);
echo toHtml($content['result']['content']);
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;
}