如何使用PHP通过JSON发送HTML元素?

如何使用PHP通过JSON发送HTML元素?,php,mysql,json,escaping,Php,Mysql,Json,Escaping,以下函数 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

以下函数

    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);
    }
我试过了

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

但似乎什么都不管用。是否可以在JSON结果中返回HTML元素和反斜杠?

您必须去掉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);
    $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);
}
试试看{
$query=$this->pdo->prepare(“从预订中选择*,其中TourID=?和dTourDate=?且状态不象‘已取消’”);
$query->execute(数组($tourId,$date));
$result=$query->fetchAll(PDO::FETCH_ASSOC);
$resultStrip=json_decode(strip_标记(json_encode($result)),true);
if(count($resultStrip)错误(“'Booking'未找到)”,$this->errCode->sqlNotBooking);
$this->success(“预订列表成功”。,(数组)$resultStrip);
}

希望有帮助。

为什么要将html元素与名称一起存储?如果需要自定义颜色,可以将其存储在单独的列中。看起来架构设计非常糟糕:(Html元素由WYSIWYG编辑器发送。我希望看到结果中的颜色等。我有一个iphone应用程序,列出查询结果,但如果它不是纯文本,则不会在前面提到的名称后显示结果。老实说,在这种情况下,我会选择不同的WYSIWYG编辑器。对你来说,最好的解决方案是重构您的代码可以实现更好的设计。不幸的是,我的判断不符合我的判断,建议您采取一些变通方法,因为您用名称保存html的逻辑已经够有害的了。那么,我们不应该在JSON结果中包含html字符吗?您完全错过了我评论的要点。祝您好运。
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));
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);
}