PHP JSON String, escape Double Quotes for JS output

后端 未结 7 1124
旧时难觅i
旧时难觅i 2020-11-29 04:44

I\'m creating a JSON string from a PHP array. I\'ve encoded it using json_encode().

$data = array(
    \'title\' => \'Example string\\\'s wit         


        
相关标签:
7条回答
  • 2020-11-29 05:27

    I just ran into this problem and the actual issue was that I forgot to add a proper application/json header before spitting out the actual JSON data.

    header('Content-Type: application/json');
    
    0 讨论(0)
  • 2020-11-29 05:28

    Use json_encode($json_array, JSON_HEX_QUOT); since php 5.3: http://php.net/manual/en/json.constants.php

    0 讨论(0)
  • 2020-11-29 05:33

    Error come on script not in HTML tags.

    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <pre><?php print_r($_POST??'') ?></pre>
    
    <form method="POST">
        <input type="text" name="name"><br>
        <input type="email" name="email"><br>
        <input type="time" name="time"><br>
        <input type="date" name="date"><br>
        <input type="hidden" name="id"><br>
        <textarea name="detail"></textarea>
        <input type="submit" value="Submit"><br>
    </form>
    <?php 
    /* data */
    $data = [
                'name'=>'loKESH"'."'\\\\",
                'email'=>'imvsrajput@demo.demo',
                'time'=>date('H:i:00'),
                'date'=>date('Y-m-d'),
                'detail'=>'Try this var_dump(0=="ZERO") \\ \\"'." ' \\\\    ",
                'id'=>123,
            ];
    ?>
    <span style="display: none;" class="ajax-data"><?=json_encode($_POST??$data)?></span>
    <script type="text/javascript">
        /* Error */
        // var json = JSON.parse('<?=json_encode($data)?>');
        /* Error solved */
        var json = JSON.parse($('.ajax-data').html());
        console.log(json)
        /* automatically assigned value by name attr */
        for(x in json){
            $('[name="'+x+'"]').val(json[x]);
        }
    </script>
    
    0 讨论(0)
  • 2020-11-29 05:38

    Another way would be to encode the quotes using htmlspecialchars:

    $json_array = array(
        'title' => 'Example string\'s with "special" characters'
    );
    
    $json_decode = htmlspecialchars(json_encode($json_array), ENT_QUOTES, 'UTF-8');
    
    0 讨论(0)
  • 2020-11-29 05:38

    I had challenge with users innocently entering € and some using double quotes to define their content. I tweaked a couple of answers from this page and others to finally define my small little work-around

    $products = array($ofDirtyArray);
    if($products !=null) {
        header("Content-type: application/json");
        header('Content-Type: charset=utf-8');
        array_walk_recursive($products, function(&$val) {
            $val = html_entity_decode(htmlentities($val, ENT_QUOTES, "UTF-8"));
        });
        echo json_encode($products,  JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
    }
    

    I hope it helps someone/someone improves it.

    0 讨论(0)
  • 2020-11-29 05:40

    I succefully just did this :

    $json = str_replace("\u0022","\\\\\"",json_encode( $phpArray,JSON_HEX_QUOT)); 
    

    json_encode() by default will escape " to \" . But it's still wrong JSON for json.PARSE(). So by adding option JSON_HEX_QUOT, json_encode() will replace " with \u0022. json.PARSE() still will not like \u0022. So then we need to replace \u0022 with \\". The \\\\\" is escaped \\".

    NOTE : you can add option JSON_HEX_APOS to replace single quote with unicode HEX value if you have javascript single quote issue.

    ex: json_encode( $phpArray, JSON_HEX_APOS|JSON_HEX_QUOT ));

    0 讨论(0)
提交回复
热议问题