2012-03-19 105 views
1

当我试图在php中回显json时,它返回\ n \ r \ t。我如何删除它们?Json返回字符

这里是我的代码:

  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_repalce之后,我越来越:

{"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|\\/|\\\"][email protected]", "", $content); 

但我不知道为什么我得到这个问题。

+0

你想删除或替换它们有效的HTML(''
)? – ManseUK 2012-03-19 13:06:13

+0

听起来像他们来自模板...你*可以*只str_re_replace他们出我想 – 2012-03-19 13:06:17

+0

我想要删除他们 – Vahan 2012-03-19 13:11:55

回答

2

除去未必要的空白:

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

将其添加到$datajson_encode之前,从$content中删除新行和制表符。使用您的内容

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

你需要逃脱两次,即'\\ n' – ManseUK 2012-03-19 13:07:49

+0

哦,我以为他只是显示哪里那些字符不是那个实际上是输出:D – scibuff 2012-03-19 13:09:39

+0

我的意思是你需要指定你的数组为'array(“\\ n”,“\\ t”,“\\ r”)'所以它发现字符 – ManseUK 2012-03-19 13:13:22