Last active
September 25, 2023 15:22
-
-
Save otnansirk/19a169e57cfcd6d5655d2f7c363a2402 to your computer and use it in GitHub Desktop.
PHP-editorjs parse to html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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