最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Encode HTML for JSON string - Stack Overflow

programmeradmin0浏览0评论

I am storing html in a mysql database as raw html. I am pulling the contents and placing it into an array as follows. The array is the json_encoded, but if there is any double quotes or urls then the javascript displaying the JSON string breaks.

Is it possible to encode html to work through JSON?

Here is an extract of what I am using

    <?php
    $rows = array();
    $sql ="SELECT html FROM table";

    try {  
        $sth = $dbh->query($sql); 
        while($r = $sth->fetch(PDO::FETCH_OBJ)) {
            $rows[] = $r;
        }
    }
    catch(PDOException $e) {  
        echo "I'm sorry, Dave. I'm afraid I can't do that. $e";  
    } 
    ?>

var json = JSON.parse('<?php print json_encode(json_encode($rows)); ?>');

The json string currently outputting throws a javascript error saying unexpected <

[{"html":"<a href="http:\/\/www.url">\n<img src="http:\/\/www.url\/img\/logo.png">\n<\/a>"}]

Please no lectures on why this may be a bad thing. This is what my client has requested specifically and I just need to know if it is possible.

I am storing html in a mysql database as raw html. I am pulling the contents and placing it into an array as follows. The array is the json_encoded, but if there is any double quotes or urls then the javascript displaying the JSON string breaks.

Is it possible to encode html to work through JSON?

Here is an extract of what I am using

    <?php
    $rows = array();
    $sql ="SELECT html FROM table";

    try {  
        $sth = $dbh->query($sql); 
        while($r = $sth->fetch(PDO::FETCH_OBJ)) {
            $rows[] = $r;
        }
    }
    catch(PDOException $e) {  
        echo "I'm sorry, Dave. I'm afraid I can't do that. $e";  
    } 
    ?>

var json = JSON.parse('<?php print json_encode(json_encode($rows)); ?>');

The json string currently outputting throws a javascript error saying unexpected <

[{"html":"<a href="http:\/\/www.url.">\n<img src="http:\/\/www.url.\/img\/logo.png">\n<\/a>"}]

Please no lectures on why this may be a bad thing. This is what my client has requested specifically and I just need to know if it is possible.

Share Improve this question edited Jun 11, 2013 at 22:04 Somk asked Jun 11, 2013 at 21:46 SomkSomk 12.1k32 gold badges100 silver badges145 bronze badges 2
  • 1 why dont you just use fetchAll() with json_encode? – Robert Commented Jun 11, 2013 at 21:49
  • idk if is a typo or actual issue but, you select column hmtl, isn't it html? – Frederik.L Commented Jun 11, 2013 at 21:59
Add a ment  | 

4 Answers 4

Reset to default 3

I agree with Mark B's answer but you could use fetchAll() and json_encode() the result of this function. Why do you use PDO Object fetching instead of array fetching?

<?php
$rows = array();
$sql ="SELECT hmtl FROM table";

try {  
    $sth = $dbh->query($sql); 
    $rows = $sth->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {  
    echo "I'm sorry, Dave. I'm afraid I can't do that. $e";  
} 
?>

var json = <?= json_encode($rows); ?>;

Moreover consider getting it via AJAX.

There's no need for the json.parse business. JSON IS valid javascript after all, so a simple

var json = <?php echo json_encode($rows); ?>;

is all you need.

The line

var json = JSON.parse('<?php print json_encode(json_encode($rows)); ?>');

...is susceptible to single quotes in the resulting JSON (and other things; you'd have to double-escape lots of stuff). But you don't need or want to do that. Do this:

var json = <?php print json_encode(json_encode($rows)); ?>;

json_encode returns valid JSON, and JSON is a subset of JavaScript object initializer syntax, and so the above will result in valid JavaScript code describing the object. If you're outputting JavaScript code, as you appear to be from that line, there's no reason to go indirectly through a string. And as you've found, there's a reason not to.

Before filling the JSON object, you could use htmlentities() to convert all applicable characters to HTML entities, and then when printing them out just use html_entity_decode() if you use an ISO standard as character set. Sorry if I might have misunderstood the question.

发布评论

评论列表(0)

  1. 暂无评论