0

我需要帮助,请我需要一些帮助。我尝试在我的网页中使用 tinyeditor jquery,编辑器的内容成功提交到数据库但带有 html 标记。现在,当我尝试从数据库中检索数据并将其显示在另一个页面上时,它会将内容与所有 html 标签一起带出。请问我该如何更正它以显示为格式化文本?谢谢。

使用的 tinyeditor 脚本

<script>
new TINY.editor.edit('editor',{
id:'input', // (required) ID of the textarea
width:584, // (optional) width of the editor
height:175, // (optional) heightof the editor
cssclass:'te', // (optional) CSS class of the editor
controlclass:'tecontrol', // (optional) CSS class of the buttons
rowclass:'teheader', // (optional) CSS class of the button rows
dividerclass:'tedivider', // (optional) CSS class of the button diviers
controls:['bold', 'italic', 'underline', 'strikethrough', '|', 'subscript', 'superscript', '|', 'orderedlist', 'unorderedlist', '|' ,'outdent' ,'indent', '|', 'leftalign', 'centeralign', 'rightalign', 'blockjustify', '|', 'unformat', '|', 'undo', 'redo', 'n', 'font', 'size', 'style', '|', 'image', 'hr', 'link', 'unlink', '|', 'print'], // (required) options you want available, a '|' represents a divider and an 'n' represents a new row
footer:true, // (optional) show the footer
fonts:['Verdana','Arial','Georgia','Trebuchet MS'],  // (optional) array of fonts to display
xhtml:true, // (optional) generate XHTML vs HTML
cssfile:'style.css', // (optional) attach an external CSS file to the editor
content:'starting content', // (optional) set the starting content else it will default to the textarea content
css:'body{background-color:#ccc}', // (optional) attach CSS to the editor
bodyid:'editor', // (optional) attach an ID to the editor body
footerclass:'tefooter', // (optional) CSS class of the footer
toggle:{text:'source',activetext:'wysiwyg',cssclass:'toggle'}, // (optional) toggle to markup view options
resize:{cssclass:'resize'} // (optional) display options for the editor resize
});

</script>

<script>
var editor = new TINY.editor.edit('editor', {
id: 'tinyeditor',
width: 500,
height: 175,
cssclass: 'tinyeditor',
controlclass: 'tinyeditor-control',
rowclass: 'tinyeditor-header',
dividerclass: 'tinyeditor-divider',
controls: ['bold', 'italic', 'underline', 'strikethrough', '|', 'subscript', 'superscript', '|',
    'orderedlist', 'unorderedlist', '|', 'outdent', 'indent', '|', 'leftalign',
    'centeralign', 'rightalign', 'blockjustify', '|', 'unformat', '|', 'undo', 'redo', 'n',
    'font', 'size', 'style', '|', 'link', 'unlink', '|', 'print'],
footer: false,
fonts: ['Verdana','Arial','Georgia','Trebuchet MS'],
xhtml: true,
cssfile: 'custom.css',
bodyid: 'editor',
footerclass: 'tinyeditor-footer',
resize: {cssclass: 'resize'}
      }
);

</script>

发布到数据库的表单

<form action="upload_poetry.php" method="post"
 enctype="multipart/form-data">
                <table width="100%" height="137" border="0" cellpadding="5" cellspacing="5">

                  <tr>
                    <td align="right" valign="top"><strong>Poem:</strong></td>
                    <td>.
                      <label for="tinyeditor"></label>
                    <textarea name="tinyeditor" id="tinyeditor" cols="45" rows="5"></textarea></td>
                </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="button" id="button" value="Submit"  onclick="editor.post()"/></td>
                </tr>

              </table>
              </form>

用于插入数据库的代码

<?php
include ('config.php');

$poem=$_POST['tinyeditor'];


$sql= "insert into poetry ( poem)values('$poem')";
$query=mysqli_query($dbC, $sql) or die(mysqli_error());
if ($query){

$msg = "Entry submitted pending Admin approval";
    header("location:poem.php?msg=$msg");
}
else{$msg = "Not submitted Please retry";
    header("location:poem.php?msg=$msg");}


?>

用于从数据库中检索的代码

<?php
                     include('config.php');
                     if (isset($_GET['id'])){
                     $id = $_GET['id'];
                     $sql = "SELECT * FROM poetry where title_id=$id";
                     $query = mysqli_query($dbC, $sql) or die(mysqli_error());
                     while($row = mysqli_fetch_array($query)){

                          $poem = $row['poem'];

                     }

                     ?>
                     <table width="50%" border="0" cellspacing="1" cellpadding="1">
                       <tr>
                       <td><strong>Poem:</strong></td>
                         <td colspan="4"><textarea id="" name="" ><?php echo $poem ?></textarea> </td>
                       </tr>

                     </table>
                       <?php
                     }
                     echo "<p><a href='index.php>Select an applicant to view his/her particulars</a></p>";
                     ?>
4

2 回答 2

0

谢谢大家。我能够通过使用如下所示的 html_entity_decode 代码解决问题 $poetry 是从数据库中检索到的数据

                         `$orig = $poetry;
                          $a = htmlentities($orig);

                        $poem = html_entity_decode($a);`

并在 div 标签而不是 textarea 中回显结果。

<div> <?php echo $poem; ?> </div> 

非常感谢您的帮助

于 2013-01-20T23:00:53.043 回答
0

看起来您的 HTML 已发送到 DB HTML 编码。如果要将其作为格式化字符串取回,请使用html_entity_decode

$textFromDB = "&lt;span&gt;Some text&lt;/span&gt;";
$decodedHTML = html_entity_decode($textFromDB);
echo $decodedHTML;

这将输出一个包含文本的 span 元素Some text,如下所示:

<span>Some text</span>

参考html_entity_decode: http: //php.net/manual/en/function.html-entity-decode.php

该页面上还有一个指向该htmlentities函数的链接,该函数处理字符串的 HTML 编码。

于 2013-01-20T01:36:08.580 回答