W3docs

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.

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:


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— editable, runs on the server

The mb_convert_encoding() function converts the string to UTF-8, ensuring apostrophes and other special characters display correctly.