php

Seeing ’ in place of an apostrophe(‘) in PHP

Recently I was seeing an issue in an email sent from my site where apostrophes were displaying as ’. After a few google searches i came across a few possible options:

HTML Encoding

As usual, Stack Overflow provided me with the background behind this, detailing the reason behind this is that I haven’t set the relevant HTML encoding, in this case UTF8.

The answer was to add a meta tag to allow the browser a chance to understand what format the page should be rendered in:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

In my instance, I already had this set.

Source: https://stackoverflow.com/questions/2292004/getting-%C3%A2%E2%82%AC-instead-of-an-apostrophe-in-php

PHPMailer Settings

As my issue was being seen in an email, I came across another stack overflow page which highlighted a few html encoding settings to set as part of building the PHPMailer object:

$mail = new PHPMailer();
$mail->CharSet = "UTF-8";

I hadn’t set this so applied the setting but the issue was still there. In my instance, as I wanted to set this on every email I was sending and so I inherited from the PHPMailer class with my own:

class MyMailer extends PHPMailer
{
    /**
     * The character set of the message.
     *
     * @var string
     */
    public $CharSet = parent::CHARSET_UTF8;
}

Source: https://stackoverflow.com/questions/2491475/phpmailer-character-encoding-issues

PHP.ini file settings

Finally I came across the solution which solved the issue for me.

There is a default character set setting within the php.ini file called: default_charset. After php 5.6 this is set by default to utf-8 however before 5.6 this needs to be set manually.

If you are running WHM on your servers, you can change the setting here:

WHM Home » Service Configuration » PHP Configuration Editor

and modify the line:

;default_charset = ""

To

default_charset = "utf-8"

MySQL Configuration

I thought it was also worth mentioning that within MySQL both the table and each column have a default charset option. Make sure that all of these are set to the charset you need. (I use utf8_general_ci – the CI just means Case Insensitive)

I hope this helps anyone who has the same problem!

1 Comment

  1. I savor, cause I found just what I used to be having a look for.
    You have ended my four day lengthy hunt! God Bless you man. Have
    a great day. Bye

Leave a Reply

Your email address will not be published. Required fields are marked *