dongyunwei8596
2015-10-05 11:22
采纳率: 100%
浏览 52
已采纳

PHP如何在变量中使用html标签/实体?

I have content that I want to display in a textarea. And on a another page in a div. The content I got from inside my database. And was inserted in the database with CKeditor. Sound easy, right?

The problem is that when I use echo or print I am getting <b>Some content</b> instead of Some content.

Believe it or not I spend 6 hours already trying to solve this problem. What is normal amount for me to spend on a problem. Only normally I am at least one step closer solving the problem. But now I am still as clueless as I started.

And yes I am using: <meta charset="utf-8">

My code(Just a basic echo, since I don`t know what to do)

<textarea ><?php echo $content; ?></textarea>

Current output:

<li><s><em><strong><span class="marker">Dit is gewijzigd?</span></strong>

But I would like this:

Dit is gewijzigd?
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongqian1925 2015-10-05 11:28
    已采纳

    You may try it like this to make the content editable

    <div contenteditable><?php echo $content; ?></div>
    

    EDIT :

    try this

    <div><?php echo htmlspecialchars_decode($content); ?></div>
    
    已采纳该答案
    打赏 评论
  • dtgvl48608 2015-10-05 11:26

    Just use strip_tags() function :

    <textarea ><?php echo strip_tags($content); ?></textarea>
    

    Docs here : http://php.net/manual/fr/function.strip-tags.php

    打赏 评论
  • dounangshen6553 2015-10-05 11:48

    You can do this using js as -- You need two times decoding since as you shown your raw data in db ..

    <textarea id='t'></textarea>
    <script>
    function getHTML(code)
    {
    var para = document.createElement("DIV");
    para.innerHTML=code;
    //return $(para).text(); 
     return para.innerText; 
    }
    document.getElementById('t').innerHTML=getHTML('<?php echo $content; ?>');
    </script>
    

    PURE PHP

    <textarea id='t'><?php echo html_entity_decode(html_entity_decode($content));?></textarea>
    
    打赏 评论
  • dongzhong1929 2015-10-05 11:51

    you can do couple of things to figure out

    1) use the html_entity_decode function which will decode the html entity which is coming from database and return the html output

    2) if the above solution doesn't work then use the first try to echo with html_entity_decode to check that does it work properly or not if it does then there is something we need to do with editor as so that editor will know that it is getting the raw html data not just plain text. i also used the FCKeditor where i have simply put the text and its working fine.

    打赏 评论

相关推荐 更多相似问题