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

php - Issue with json decode in Wordpress - value is not returning consistently

programmeradmin2浏览0评论

I have a json file I am displaying the contents of on my wordpress site. The issue stems from the fact that some of the values I'm returning I need to sort through later, and while that value is being echoed as text, an apostrophe is being displayed as an apostrophe, however if I am echoing the exact same value inside HTML(like as a data attribute or in javascript) it returns as a single quote.

I need this value to return the same format no matter where it is being echoed. This is because this data will later be used to filter through a large data table.

<?php $content = file_get_contents('jsonfilepath');
$x = json_decode($content);

$array = array(); 
foreach($x as $row) {

             $value = $row->name;
                        if(!in_array($name, $array)){
                               array_push($array,$value);
                        }

                    };

                    sort($array);
                    foreach( $array as $name ){

                        $name = mb_convert_encoding($name,"UTF-8");
                        $name = ucwords(strtolower($name));
                        $name = html_entity_decode($name, ENT_QUOTES, "UTF-8");
                        $name = str_replace("&#8217;","'", $name); 
                        $name = htmlspecialchars_decode(ucwords(strtolower($name)));
                        $name = implode('/', array_map('ucfirst', explode('/', $name)));
                        echo '<option value="'.utf8_decode($name).'">'.utf8_decode($name).'</option>';
                    }

This returns

<option value="Chicago/O'hare">Chicago/O&#8217;hare</option>

I am almost at the point of running a script after the page loads to replace all instances of this apostrophe, but there has to be a serverside way to handle this.

I have a json file I am displaying the contents of on my wordpress site. The issue stems from the fact that some of the values I'm returning I need to sort through later, and while that value is being echoed as text, an apostrophe is being displayed as an apostrophe, however if I am echoing the exact same value inside HTML(like as a data attribute or in javascript) it returns as a single quote.

I need this value to return the same format no matter where it is being echoed. This is because this data will later be used to filter through a large data table.

<?php $content = file_get_contents('jsonfilepath');
$x = json_decode($content);

$array = array(); 
foreach($x as $row) {

             $value = $row->name;
                        if(!in_array($name, $array)){
                               array_push($array,$value);
                        }

                    };

                    sort($array);
                    foreach( $array as $name ){

                        $name = mb_convert_encoding($name,"UTF-8");
                        $name = ucwords(strtolower($name));
                        $name = html_entity_decode($name, ENT_QUOTES, "UTF-8");
                        $name = str_replace("&#8217;","'", $name); 
                        $name = htmlspecialchars_decode(ucwords(strtolower($name)));
                        $name = implode('/', array_map('ucfirst', explode('/', $name)));
                        echo '<option value="'.utf8_decode($name).'">'.utf8_decode($name).'</option>';
                    }

This returns

<option value="Chicago/O'hare">Chicago/O&#8217;hare</option>

I am almost at the point of running a script after the page loads to replace all instances of this apostrophe, but there has to be a serverside way to handle this.

Share Improve this question asked Nov 22, 2019 at 22:23 BenlovBenlov 215 bronze badges 4
  • Where do you call the code? In a shortcode? A template file? And I don't see any WP-specific functions in your code. Have you tried var_dump( $name ); at the top of the foreach? – Sally CJ Commented Nov 22, 2019 at 22:55
  • I'm calling it in a template file. – Benlov Commented Nov 22, 2019 at 23:20
  • How are you calling it? What's the code in your template? And you didn't answer the var_dump question. – Sally CJ Commented Nov 22, 2019 at 23:36
  • string(14) &#8220;Chicago/O&#8217;hare&#8221; is what it returns – Benlov Commented Nov 25, 2019 at 14:53
Add a comment  | 

1 Answer 1

Reset to default 0

I found the issue. This was being echoed in the_content and Wordpress's texturize was converting to smartquotes. I've disabled that on The Content and that seems to have solved the issue.

发布评论

评论列表(0)

  1. 暂无评论