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("’","'", $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’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("’","'", $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’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 |1 Answer
Reset to default 0I 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.
var_dump( $name );
at the top of theforeach
? – Sally CJ Commented Nov 22, 2019 at 22:55var_dump
question. – Sally CJ Commented Nov 22, 2019 at 23:36