Appearance
Getting ’ instead of an apostrophe(') in PHP
This can occur if your PHP code is using the ISO-8859-1 character set instead of UTF-8. To fix this issue, you can change the character set to UTF-8 in your PHP script by including the following line at the top of your script:
console
header('Content-Type: text/html; charset=UTF-8');You can also change the character set in your HTML by including the following line within the <head> section of your HTML document:
<meta charset="UTF-8">
Additionally, ensure your database connection uses UTF-8 (e.g., mysqli_set_charset($conn, 'utf8mb4'); or setting the charset in your PDO DSN).
Here's a full example of how to handle the issue of getting ’ instead of an apostrophe (') in PHP:
Example of how to handle the issue of getting ’ instead of an apostrophe in PHP
php
<?php
// Set default charset to UTF-8
ini_set('default_charset', 'UTF-8');
// Simulate a string that was incorrectly decoded as ISO-8859-1
$text = "Here’s an example of using an apostrophe in PHP";
// Convert the string from ISO-8859-1 to UTF-8 encoding
$text = mb_convert_encoding($text, 'UTF-8', 'ISO-8859-1');
echo $text;
?>The mb_convert_encoding() function converts the string to UTF-8, ensuring apostrophes and other special characters display correctly.