如何使用PHP通过JSON发送HTML元素? [英] How to send HTML elements through JSON using PHP?

查看:117
本文介绍了如何使用PHP通过JSON发送HTML元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下功能

    try{
        $query =  $this->pdo->prepare("SELECT * FROM bookings WHERE TourID = ? AND dTourDate = ? and Status NOT LIKE 'Cancelled'");
        $query->execute(array($tourId,$date));
        $result = $query->fetchAll(PDO::FETCH_ASSOC);
        if(count($result)<1)
            $this->error("'Booking' Not Found.",$this->errCode->sqlNotBooking);
        $this->success("Booking List Success.",(array) $result);
    }

向我返回此:

 TotalPrice":"0.00","GuestName":"Bryan Pedrochi<\/span>","ContactNumber":"042214"...

mysql中的GuestName列如下所示

The GuestName column in mysql looks like this

<span style="background-color: rgb(255, 255, 0);">Bryan Pedrochi</span>

我不是很擅长编程,我不确定该怎么做,但是我相信我必须在双引号之前加上反斜杠,这样才能获得正确的结果

I am not very good in programming and I am not sure how to do it but I believe I have to place backslash before the double quote in regarding to have a proper result like this

TotalPrice":"0.00","GuestName":"<style=\"background-color: rgb(255, 255, 0);\">Bryan Pedrochi</span>","ContactNumber":"042214"...

所以,我尝试了

$add= $query->fetchAll(PDO::FETCH_ASSOC);
$string=serialize($add); 
$result=addslashes($string); 

我尝试过

$this->success("Booking List Success.",(array) htmlentities($result));

,但似乎没有任何效果.是否可以在JSON结果中返回HTML元素和反斜杠?

but nothing seems to work. Is it possible to return HTML elements and backslashes in JSON results?

推荐答案

您必须剥离html标签.

you have to strip the html tags.

try{
    $query =  $this->pdo->prepare("SELECT * FROM bookings WHERE TourID = ? AND dTourDate = ? and Status NOT LIKE 'Cancelled'");
    $query->execute(array($tourId,$date));
    $result = $query->fetchAll(PDO::FETCH_ASSOC);
    $resultStrip = json_decode(strip_tags(json_encode($result)), true);
    if(count($resultStrip )<1)
        $this->error("'Booking' Not Found.",$this->errCode->sqlNotBooking);
    $this->success("Booking List Success.",(array) $resultStrip);
}

希望有帮助.

这篇关于如何使用PHP通过JSON发送HTML元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆