简体   繁体   中英

json encode - special characters

I am using the json_encode function in a fairly simple quote system I have:

<script type='text/javascript'>
postQuote[<?php echo json_encode($post['post_id']); ?>] = <?php echo json_encode($post['post_text']); ?>
</script>

Which outputs this:

postQuote["11241"] = "&amp;Kappa;&amp;alpha;&amp;lambda;&amp;eta;&amp;sigma;&amp;pi;\u03ad&amp;rho;&amp;alpha; &amp;alpha;&amp;lambda;\u03ac&amp;nu;&amp;iota;&amp;alpha; &amp;mu;&amp;omicron;&amp;upsilon;. \u0388&amp;chi;&amp;omega; &amp;kappa;\u03ac&amp;pi;&amp;omicron;&amp;iota;&amp;epsilon;&amp;sigmaf; &amp;alpha;&amp;pi;&amp;omicron;&amp;rho;\u03af&amp;epsilon;&amp;sigmaf;. \r\n1&amp;omicron;&amp;nu;: &amp;Eta; &amp;pi;&amp;rho;\u03ac&amp;sigma;&amp;iota;&amp;nu;&amp;epsilon;&amp;sigmaf; &amp;delta;&amp;iota;&amp;alpha;&amp;phi;&amp;eta;&amp;mu;\u03af&amp;sigma;&amp;epsilon;&amp;iota;&amp;sigmaf; &amp;mu;&amp;omicron;&amp;upsilon; &amp;delta;\u03af&amp;nu;&amp;omicron;&amp;upsilon;&amp;nu; $0.01; &amp;Alpha;&amp;nu; &amp;nu;&amp;alpha;&amp;iota; &amp;tau;\u03cc&amp;tau;&amp;epsilon; &amp;mu;\u03ac&amp;lambda;&amp;lambda;&amp;omicron;&amp;nu; \u03ad&amp;chi;&amp;omega; &amp;pi;&amp;rho;\u03cc&amp;beta;&amp;lambda;&amp;eta;&amp;mu;&amp;alpha; &amp;gamma;&amp;iota;&amp;alpha;&amp;tau;\u03af &amp;epsilon;\u03af&amp;chi;&amp;alpha; $0.60 &amp;pi;\u03ac&amp;tau;&amp;eta;&amp;sigma;&amp;alpha; &amp;kappa;&amp;alpha;&amp;iota; &amp;tau;&amp;eta;&amp;sigmaf; 4 &amp;pi;&amp;rho;\u03ac&amp;sigma;&amp;iota;&amp;nu;&amp;epsilon;&amp;sigmaf; &amp;kappa;&amp;alpha;&amp;iota; &amp;pi;\u03ae&amp;gamma;&amp;alpha; &amp;mu;\u03cc&amp;nu;&amp;omicron; $0.062.\r\n2&amp;omicron;&amp;nu;: \u03ad&amp;chi;&amp;omega; 5 rented ref. &amp;Tau;\u03ce&amp;rho;&amp;alpha; &amp;mu;&amp;pi;&amp;omicron;&amp;rho;\u03ce &amp;nu;&amp;alpha; &amp;alpha;&amp;gamma;&amp;omicron;&amp;rho;\u03ac&amp;sigma;&amp;omega; \u03ac&amp;lambda;&amp;lambda;&amp;omicron;&amp;upsilon;&amp;sigmaf; 3 &amp;mu;&amp;epsilon; $0.6 &amp;lambda;\u03ad&amp;tau;&amp;epsilon; &amp;nu;&amp;alpha; &amp;tau;&amp;omicron;&amp;upsilon;&amp;sigmaf; &amp;alpha;&amp;gamma;&amp;omicron;&amp;rho;\u03ac&amp;sigma;&amp;omega; &amp;eta; &amp;nu;&amp;alpha; &amp;pi;\u03ac&amp;omega; $1 &amp;kappa;&amp;alpha;&amp;iota; &amp;nu;&amp;alpha; &amp;pi;\u03ac&amp;rho;&amp;omega; \u03ac&amp;lambda;&amp;lambda;&amp;omicron;&amp;upsilon;&amp;sigmaf; 5;\r\n\r\n&amp;Alpha;&amp;upsilon;&amp;tau;\u03ac &amp;pi;&amp;rho;&amp;omicron;&amp;sigmaf; &amp;tau;&amp;omicron; &amp;pi;&amp;alpha;&amp;rho;\u03cc&amp;nu;."    

The above text is (Greek):

Καλησπέρα αλάνια μου. Έχω κάποιες απορίες. 
1ον: Η πράσινες διαφημίσεις μου δίνουν $0.01; Αν ναι τότε μάλλον έχω πρόβλημα γιατί είχα $0.60 πάτησα και της 4 πράσινες και πήγα μόνο $0.062.
2ον: έχω 5 rented ref. Τώρα μπορώ να αγοράσω άλλους 3 με $0.6 λέτε να τους αγοράσω η να πάω $1 και να πάρω άλλους 5;

Αυτά προς το παρόν.

Now, to my understanding it encodes the string to HTML characters, Therefore before I output it, I do:

$(".quote").click(function() {
    $("#text").val($("<div>").html("[quote]" + postQuote[$(this).attr('name')] + "[/quote]").text());
});

Which doesn't help. The output is still the same as above.

What am I doing wrong?

EDIT:

$s = $dbh->prepare("
    SELECT t.*, c.forum_name,
    (SELECT count(*) FROM forum_posts WHERE topic_id=t.topic_id AND post_deleted=0) as posts
    FROM forum_topics t
    JOIN forum_cats c on c.forum_id = t.forum_id
    WHERE t.topic_id=:id
    ");
$s->bindParam(':id', $id);
$s->execute();
$q = $s->fetch(PDO::FETCH_ASSOC);

    while ($post = $p->fetch(PDO::FETCH_ASSOC)) {

//Postquote inside here.
}               

My table forum_posts is utf8_general_ci

2nd edit:

It seems like a problem with GREEK letters? Any suggestions?

You should use htmlspecialchars(). This with proper options will make your string to be valid HTML. json_encode is to get json string from PHP value. You are using this with strings, so in fact you will not want to have json. Particularly I don't see why you use it with post_id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM