Chrome Extension

Editorjs blocks - JSON to HTML PHP

<?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;
    }
?>
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool
Output
return HTML string from JSON blocks provided by Editorjs

The code can be used to get the HTML from JSON data blocks provided by Editorjs. We have used simple PHP code to using switch case to convert that.

Search Index Data (The code snippet can also be found with below search text)

Get HTML string from Editorjs JSON using PHP
Was this helpful?
0 Comments
Join Devsheet Ask a Question