I am having an issue with some rows in my SQL database. There exists rows in the database with columns that have characters like these:
Macy’s “Buy 1 & We’ll Donate 1â€Â...
I already know that I messed up when I inserted these rows into the table. I have a .csv
file with many rows with those UTF-8 characters which is populated from scraping a few web pages. I am aware that I should not be using utf8_decode to decode data from MySQL and that I should have had the correct encoding when I inserted the data into the table. However, re-scraping the data and reinserting it would be a big inconvenience so I am seeking this solution for now.
When I try to use utf8_decode($row["description"])
where $row["description"]
is the string above, the output is nothing and I get an error. I have code that looks like this:
$events[] = array(
'description' => utf8_decode($row['description']),
// some other fields...
);
When I try to echo the php array into javascript like this:
var events = <?php echo json_encode($events); ?>;
I'm getting an error that says events
is undefined in the console.
The issue is that this is only happening for a select few queries and I can echo the result of utf8_decode
with no error.
What I mean by this is that I have rows in the table with similar (if not identical) characters:
New Chipotle ‘Guac Hunter’ Experience Re...
When I utf8_decode
the string, it comes out nicely:
New Chipotle ‘Guac Hunter’ Experience Rewards Players with Free Chips and Guacamole
I also tried echoing utf8_decode($row["description"]) for these select queries that were returning nothing to see what would happen. The decoded string was echoed correctly to the page. All of the weird characters were replaced with human-readable characters. I changed the $events[]
array's description
attribute to just $row["description"]
and added this:
echo utf8_decode($row["description"]);
Here is what the output looks like when I echo the utf8_decode
d version of the row. The upper half is the decoded version, which is correctly decoded as the apostrophes are correctly shown. However, in the bottom half you can see that the apostrophes are replaced with the odd a-hat character.
This makes it even more confusing for me -- why can I echo the utf8_decoded
version of $row["description"]
but can't put it into an array?