# How to parse HTML formated

**URL:** <https://discourse.cakephp.org/t/how-to-parse-html-formated/8652>\
**Category:** Need Help\
**Created:** [November 23, 2020, 1:40pm UTC](https://discourse.cakephp.org/t/how-to-parse-html-formated/8652 "2020-11-23T13:40:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arizzoye](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@arizzoye](https://discourse.cakephp.org/u/arizzoye)\
**Post date:** [November 23, 2020, 1:40pm UTC](https://discourse.cakephp.org/t/how-to-parse-html-formated/8652/1 "2020-11-23T13:40:29Z")

</div>

I have trouble while rendering display article for look beautifull. Saving article have sanitize with used `trim(stripslashes($article));`

And I want to display article use HTML code by parse saving before, the goal is easy and comfort to read article.

```auto
in Article controller

public function view($id = null)
    {
        $article = $this->Articles->get($id, [
            'contain' => ['Users', 'Comments', 'Tags']
        ]);
		$parsearticle = $this->Articles->find()->extract('Isi');
		return $this->response
			->withType('application/json')
			->withStringBody(json_encode([
				'parseHTML' => $parsearticle,
			]));

        $this->set('article', $article);
    }

in method
public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
 {
   foreach($data as $key => $article) {
	  if(is_string($article)) {
		$data[$key] = trim(stripslashes($article));
	  }
     }	
   }

in ctp

<div id="parseHTML">
</div>
<p>
   <?php echo $this->Text->autoParagraph(h($article->Isi))."\n"; ?>
</p>

<!-- parser HTML -->
<script type="text/javascript">
 jQuery(document).ready(function() {
   var tampil = $("#parseHTML");
   var parse = '<?= Router::url(["controller" => "articles","action" => "view"]); ?>';
   var konv = $.parseHTML(parse);
   
   $.ajax({
      type:'post',
	  url:parse,
	  data:'&type=parseHTML',
	  dataType:'json',
	  success:function(){
		tampil.append(konv);
	});
  });
</script>
<!-- //parser HTML -->

```

the error shown display below

```auto
0	"kebersihan kandang harus dijaga"
1	"penyakit ini sangat berbahaya, perhatikan kebersihan lingkungan pengobatan vaksinnya membutuhkan aktu lama. Penyakit ini menimbulkan cacat janin"
2	"penyakit ini disebabkan jamur, jamur toxic ini banyak tersebar di tempat jorok dan kumuh"
3	"penyakit ini disebabkan jamur, jamur ini banyak tersebar di tempat-tempat jorok"
4	"kucing harus dirawat, sebagai tanda kebersihan pemiliknya juga"
5	"<p><span style=\"backgro…izen<br></font>s<br></p>"
6	"<p><font color=\"#000000\"><span style=\"background-color: rgb(255, 255, 255);\" wfd-id=\"58\">Selamat malam semuanya</span></font></p><p><font color=\"#000000\"><span style=\"background-color: rgb(255, 255, 255);\" wfd-id=\"57\"><br></span></font></p><table class=\"table table-bordered\"><tbody><tr><td><font color=\"#000000\">Nama</font><br></td><td>l<font color=\"#000000\">Alamat</font><br></td></tr><tr><td><font color=\"#000000\">Aris Setiawan</font><br></td><td><span style=\"background-color: rgb(255, 255, 255);\" wfd-id=\"56\"><font color=\"#000000\">Tangerang</font></span><br></td></tr></tbody></table><p><font color=\"#000000\"><span style=\"background-color: rgb(255, 255, 255);\" wfd-id=\"55\"><br></span></font><span style=\"background-color: rgb(255, 255, 255);\" wfd-id=\"54\"><font color=\"#000000\">Kita semua pernah <u>memelihara</u> <b>kucing</b> yah, jangan lupa dijaga kebersihannya<br></font></span></p><p><span style=\"background-color: rgb(255, 255, 255);\" wfd-id=\"53\"><font color=\"#000000\"><br></font></span></p><p><span style=\"background-color: rgb(255, 255, 255);\" wfd-id=\"52\"><font color=\"#000000\"><br></font></span><br></p>"
7	"<blockquote><p><span style=\"background-color: rgb(247, 198, 206);\"><font color=\"#000000\">Merawat Bulu Kucing Itu Sangat Mudah</font></span></p></blockquote><p><font color=\"#000000\"><br></font></p><p style=\"margin-left: 25px;\"><font color=\"#000000\">Seperti manusia yang merawat rambutnya dengan penuh perhatian, secara rutin saat mandi dikeramasin, dishampoin.</font></p><p style=\"\"><font color=\"#000000\">Pun begitu kepada binatang peliharaan kesayangan kita kucing, juga harus dirawat bulu-bulunya, jangan dibiarkan rontok,</font></p><p style=\"\"><font color=\"#000000\">banyak kutunya, bulu-bulunya bau apek penuh debu.</font></p><p style=\"\"><font color=\"#000000\"><br></font></p><p style=\"margin-left: 25px;\"><font color=\"#000000\">Cara merawat bulu kucing, yaitu rutin dikeramasin, rutin disisirin, rutin diberi wangi2-an, rutin diberi antiseptik. Insya Allah</font></p><p style=\"\"><font color=\"#000000\">kucing itu jadi terlihat bersih, lucu dan imut<br></font><br></p>"

```

thanx for want to help me

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [November 23, 2020, 4:49pm UTC](https://discourse.cakephp.org/t/how-to-parse-html-formated/8652/2 "2020-11-23T16:49:35Z")

</div>

Do you want to save a WYSIWYG html content?  
If yes, you shouldn’t use stripslashes. You can use [HTML purifier](https://github.com/ezyang/htmlpurifier) and simply echo the output.

If you want just to show the plain text as paragraph then don’t use stripslashes, just autoParagraph

I don’t understand the error message

---

<div class="post-metadata">

**Author:** ![arizzoye](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@arizzoye](https://discourse.cakephp.org/u/arizzoye)\
**Post date:** [November 24, 2020, 3:42am UTC](https://discourse.cakephp.org/t/how-to-parse-html-formated/8652/3 "2020-11-24T03:42:10Z")

</div>

really that’s running good, not error, just display JSON.

I have read tutorial on  
[https://discourse.cakephp.org/t/how-to-make-simple-jquery-ajax-in-cakephp-3-7/5834](https://discourse.cakephp.org/t/how-to-make-simple-jquery-ajax-in-cakephp-3-7/5834)

and

> **[jQuery.parseHTML() | jQuery API Documentation](https://api.jquery.com/jquery.parsehtml/)**

`in ctp`  
the logic that I’m expect is value of variable `$article->Isi`  
send to jQuery to parse HTML formated

```auto
<div id="parseHTML">
</div>
<!-- parser HTML -->
<script type="text/javascript">
 jQuery(document).ready(function() {
   var tampil = $("#parseHTML");
   var parse = '<?= Router::url(["controller" => "articles","action" => "view"]); ?>';
   var konv = $.parseHTML(parse);
   
   $.ajax({
      type:'post',
	  url:parse,
	  data:'&type=parseHTML',
	  dataType:'json',
	  success:function(){
		tampil.append(konv);
	});
  });
</script>
<!-- //parser HTML -->

```

but the goal, doesn’t I’m expect. I wanto view article there’s formated HTML content identation, paragraf, align justify, bold and underline. For easy to reading

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 24, 2020, 2:44pm UTC](https://discourse.cakephp.org/t/how-to-parse-html-formated/8652/4 "2020-11-24T14:44:56Z")

</div>

So, you have formatted HTML in your database, and you want to display it that way in the view? Why can you not just output exactly what you read from the database?
