как преобразовать код json в HTML, используя php - PullRequest
0 голосов
/ 19 апреля 2020

Я хочу преобразовать ниже JSON в HTML в 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"
  }
]

Я пишу эту функцию, но не работает хорошо

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;
} 
?>

Список не работает точно хорошо (ul, ol)

я хочу, чтобы результат был таким

(Это демонстрационный пост Test Bold Bold and Itali c Itali c

  • Item 1
  • Item 2
  • Item 3
    1. Item
    2. Item
    3. Item

)

Я пробовал это с пером, но перо не работает для списка загрузки, заголовок, изображение и ...

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...