'how to convert a json to HTML code using php
I want convert below JSON to HTML in PHP:
[
{
"insert": "This is demo post "
},
{
"insert": "Test Bold",
"attributes": {
"b": true
}
},
{
"insert": "Bold and Italic ",
"attributes": {
"b": true,
"i": true
}
},
{
"insert": "Italic",
"attributes": {
"i": true
}
},
{
"insert": "\nText with H3"
},
{
"insert": "\n",
"attributes": {
"heading": 3
}
},
{
"insert": "Text With H2 "
},
{
"insert": "\n",
"attributes": {
"heading": 2
}
},
{
"insert": "Text With H1"
},
{
"insert": "\n",
"attributes": {
"heading": 1
}
},
{
"insert": "Another Text\n Item 1"
},
{
"insert": "\n",
"attributes": {
"block": "ul"
}
},
{
"insert": "Item 2"
},
{
"insert": "\n",
"attributes": {
"block": "ul"
}
},
{
"insert": "Item 3"
},
{
"insert": "\n",
"attributes": {
"block": "ul"
}
},
{
"insert": "Item"
},
{
"insert": "\n",
"attributes": {
"block": "ol"
}
},
{
"insert": "Item"
},
{
"insert": "\n",
"attributes": {
"block": "ol"
}
},
{
"insert": "Item"
},
{
"insert": "\n",
"attributes": {
"block": "ol"
}
},
{
"insert": "The Quote Look Like this"
},
{
"insert": "\n",
"attributes": {
"block": "quote"
}
},
{
"insert": "and The Codes Like This"
},
{
"insert": "\n",
"attributes": {
"block": "code"
}
},
{
"insert": "",
"attributes": {
"embed": {
"type": "hr"
}
}
},
{
"insert": "\n and this is HR (Line) \n"
},
{
"insert": "",
"attributes": {
"embed": {
"type": "image",
"source": "https://dl.example.net/5we4936ds6680572_es2dQgAmasdOZn2MG.jpg"
}
}
},
{
"insert": "\nThis is an Image\n"
},
{
"insert": "This is Hyper Link",
"attributes": {
"a": "https://apexteam.net"
}
},
{
"insert": "Another Text \n"
}
]
i write this function but not work good
function ConvertJsonToHtml($content) {
$html = '';
$close = '';
for ($i = 0; $i < count($content); $i++) {
if (isset($content[$i]['insert'])) {
// Bold
if ($content[$i]['attributes']['b']) {
$html .= "<b>";
}
// Italic
if ($content[$i]['attributes']['i']) {
$html .= "<i>";
}
// H1 H2 H3
if (isset($content[$i + 1]['attributes']['heading'])) {
$html .= "<h" . $content[$i + 1]['attributes']['heading'] . ">";
$close = "</h" . $content[$i + 1]['attributes']['heading'] . ">";
}
// Ul, Ol, Code, quote
if (isset($content[$i + 1]['attributes']['block'])) {
if($content[$i + 1]['attributes']['block'] == 'ul' || $content[$i + 1]['attributes']['block'] == 'ol') {
$html .= "<{$content[$i + 1]['attributes']['block']}><li>";
$close = "</li></{$content[$i + 1]['attributes']['block']}>";
} else if($content[$i+1]['attributes']['block'] == "quote"){
$html .= "<blockquote>";
$close = "</blockquote>";
} else if($content[$i+1]['attributes']['block'] == "code"){
$html .= "<code style='font-family: Lalezar, sans-serif;width: 100%;
position: absolute;
direction: ltr;
text-align: left;
background-color: #222;
color: #fff;'>";
$close = "</code><br>";
}
}
//Link
if (isset($content[$i]['attributes']['a'])) {
$html .= "<a target='_blank' href='{$content[$i]['attributes']['a']}'>";
}
// Insert
$html .= str_replace("\n", "\r\n", $content[$i]['insert']);
//Link
if (isset($content[$i]['attributes']['a'])) {
$html .= "</a>";
}
// Close
if (!empty($close)) {
$html .= $close;
$close = '';
}
// Bold
if ($content[$i]['attributes']['b']) {
$html .= "</b>";
}
// Italic
if ($content[$i]['attributes']['i']) {
$html .= "</i>";
}
}
if (isset($content[$i]['attributes']['embed']) && $content[$i]['attributes']['embed']['type'] == "image") {
$html .= "<center><img src='" . $content[$i]['attributes']['embed']['source'] . "' style='width:350;height:auto;margin: auto;position: center;left: 50%;'></center>";
}
}
return $html;
}
?>
The List not working exactly good (ul, ol)
i want the result be like this
( This is demo post Test Bold Bold and Italic Italic
- Item 1
- Item 2
- Item 3
- Item
- Item
- Item
)
I tried this with quill but quill does not work for load list, heading, image and ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|