Json返回字符

Json returns characters

本文关键字:字符 返回 Json      更新时间:2023-09-26

当我尝试在php中回显json时,它会返回''n''r''t。如何移除它们?

这是我的代码:

            ob_start();
                $this->load->view('competition/template',$q);
                $content = ob_get_clean();
                $data['content'] = $content;
             echo json_encode($data);

我得到了:

</table>{"ok":1,"content":"<table>'r'n   <tr>'r'n't    <td>Competitor name<'/td>'r'n't    <td><input type='"text'" name='"competitor_name[2]'" '/><'/td>'r'n   <'/tr>'r'n   <tr>'r'n   <'/tr>'r'n   <tr>'r'n   <'/tr>'r'n   <tr>'r'n   <'/tr>'r'n<'/table>"}

这是template.php文件:

 <table>
   <tr>
        <td><?php echo $this->__('Competitor name');?></td>
        <td><input type="text" name="competitor_name[<?php echo $id?>]" /></td>
   </tr>
</table>

在str_replace或preg_repace之后,我得到:

{"ok":1,"content":"<table>   <tr>    <td>Competitor name<'/td>    <td><input type='"text'" name='"competitor_name[1]'" '/><'/td>   <'/tr> <'/table>"}

现在我的问题是''

谢谢。

好的,我找到了anwser。这是:

 $content = preg_replace("@[''r|''n|''t|''/|'''"]+@", "", $content);

但我不确定我为什么会遇到这个问题。

$data['content'] = str_replace( array("'n","'t","'r"), "", $content );

使用从内容中删除不必要的空白

$content = preg_replace("@[''r|''n|''t]+@", "", $content);
$data['content'] = $content;
echo json_encode($data);

在将其添加到$datajson_encode之前,从$content中删除新行和制表符。