Skip to content

Instantly share code, notes, and snippets.

@otnansirk
Last active September 25, 2023 15:22
Show Gist options
  • Save otnansirk/19a169e57cfcd6d5655d2f7c363a2402 to your computer and use it in GitHub Desktop.
Save otnansirk/19a169e57cfcd6d5655d2f7c363a2402 to your computer and use it in GitHub Desktop.
PHP-editorjs parse to html
<?php
function jsonToHtml($jsonStr) {
$obj = json_decode($jsonStr);
$html = '';
foreach ($obj->blocks as $block) {
switch ($block->type) {
case 'paragraph':
$html .= '<p>' . $block->data->text . '</p>';
break;
case 'header':
$html .= '<h'. $block->data->level .'>' . $block->data->text . '</h'. $block->data->level .'>';
break;
case 'raw':
$html .= $block->data->html;
break;
case 'list':
$lsType = ($block->data->style == 'ordered') ? 'ol' : 'ul';
$html .= '<' . $lsType . '>';
foreach($block->data->items as $item) {
$html .= '<li>' . $item . '</li>';
}
$html .= '</' . $lsType . '>';
break;
case 'code':
$html .= '<pre><code class="language-'. $block->data->lang .'">'. $block->data->code .'</code></pre>';
break;
case 'image':
$html .= '<div class="img_pnl"><img src="'. $block->data->file->url .'" /></div>';
break;
default:
break;
}
}
return $html;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment