My Journey to PHP: Converting web​fo​cus​de​sign​.com

As I hinted in an ear­lier post about my move to WordPress, one of the rea­sons for that deci­sion was to help me learn PHP. I am well on my way and I have now cre­ated one client site with PHP (sim­ple stuff server-​side so it was easy) and I am now in the process of con­vert­ing my busi­ness site (web​fo​cus​de​sign​.com) which is slightly more com­plex but still easy. The pri­mary rea­son for that move is that I want to con­vert the blogs there to WordPress (2 blogs because i need one in French and one in English) and want to move the site to Mediatemple (mt). For now, the process has been… frustrating…

Fighting With Charset and Encoding in PHP

Coming from ColdFusion, I must say that I’m used to hav­ing an eas­ier time get­ting things done and for simple/​obvious things to just work out of the box. For a good part of the evening last night and part of this morn­ing, I’ve been fight­ing to get French accented char­ac­ters to dis­play cor­rectly in the PHP ver­sion of web​fo​cus​de​sign​.com. Before going fur­ther, you need to know that the rea­son I use CF or PHP even on the sim­plest sites is to use includes for code reuse. I use includes to be able to con­trol all the code in a document’s <head> for exam­ple from one file and I set page spe­cific data like titles, meta-​description and meta-​keywords using vari­ables I set on each page.

Now what does this have to do with accented char­ac­ters dis­play? Well, I usu­ally place the <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8″ /​> meta tag in that head include. Then, in ColdFusion, I add the <cfpro­cess­ingdi­rec­tive pageencoding=“utf-8″> tag as the first ele­ment on all pages and it just works. The meta tag doesn’t actu­ally have any effect but if I remove the cfpro­cess­ingdi­rec­tive, the dis­play of accented char­ac­ters is gar­bled. I put it back it’s OK. Direct cor­re­la­tion, just works as expected.

So, in PHP, I tried the same strat­egy, I used the same include file (includ­ing the Content-​Type meta tag), changed the few CF bits of code to their PHP equiv­a­lents and I placed the PHP header(‘Content-Type: text/​html; charset=utf-8′); com­mand as the first line of code on my page (after the open­ing <?php of course). It didn’t work… or I should say, it half worked. All the straight HTML text that came from other includes (footer, side col­umn repeat­ing ele­ments, etc) dis­played OK. But the actual HTML text on the page (which didn’t come from an include, was not out­put by a PHP com­mand and didn’t come from a data­base) dis­played gar­bled accented char­ac­ters. I was stumped (and still am).

Google was absolutely no help when I searched for PHP charset and encod­ing issues as pretty much all the info I found on the mat­ter dealt with data­base out­put or PHP string manip­u­la­tion com­mands and get­ting those to dis­play as UTF-​8. But it was my pure HTML text that dis­played wrong. I was get­ting REALLY frustrated.

Then, I thought of com­par­ing the code to my just pub­lished client site (100% French with no dis­play issues) and real­ized that, for rea­sons I can’t remem­ber as I coded the basic tem­plates months ago, I’d placed the <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8″ /​> meta tag directly on the page (and not in an include) right after the open­ing <head> tag which is the SAME EXACT SPOT it ends up at any­way as it was the first line of code in my include. The interpreted/​processed code you see in the browser in a view source looks exactly the same. I then had a light­bulb moment and looked for encod­ing issues related to Dreamweaver since it’s my pri­mary cod­ing envi­ron­ment. I found out the fol­low­ing. If it can help some­one else my frus­tra­tion won’t have been in vain…

The Solution?

On the files that work cor­rectly (Content-​Type meta tag directly in file and not in my head include), if I go to Modify>Page Properties in Dreamweaver and look at the Title/​Encoding sec­tion, the encod­ing reads/​is set as Unicode (UTF-​8). For pages with the meta tag in the include, Dreamweaver doesn’t see it so it defaults to “Western-​European” or some­thing other than UTF-​8 anyway.

So my guess is that (think­ing as I’m writ­ing), Dreamweaver prob­a­bly saves the file dif­fer­ently and Apache or PHP prob­a­bly serve it dif­fer­ently and no mat­ter what the meta tag or head­ers say, accented char­ac­ters dis­played wrong. But what still has me per­plexed is that, if I do the same test on the ColdFusion ver­sion of the index file in the site, Dreamweaver sees it as UTF-​8 even if the meta tag is “hid­den” in an include. My guess here is that, I prob­a­bly had saved that file a few times before mov­ing the code to an include as I was first devel­op­ing that site. I usu­ally build the first “tem­plate” of a new site with­out includes but, for the PHP ver­sion, it was just a mat­ter of con­vert­ing to PHP so most of the work was already done.

If any­one can shed some more light on this, please feel free!

If you liked this article, get updates (it’s free).

, , , , ,

One Response to My Journey to PHP: Converting web​fo​cus​de​sign​.com

  1. Michel June 20, 2010 at 9:45 am #

    Good guess!

    Dw, indeed, saves a file that does *not* have an encod­ing spec­i­fied in it (using a META tag), with a default encod­ing, set up in Edit > Preferences > New Document > Default Encoding.

    Also, good to know that Dw will open an exist­ing file accord­ing to this same set­ting (*Use when open­ing exist­ing files that don’t spec­ify an encod­ing*), if the file has no META tag for encod­ing in it. It will then save the file accord­ing to this setting!

    And also, if the file has a “hid­den” encod­ing spec­i­fied (for exam­ple, a META tag inside an HTML com­ment), it will open and/​or save the file accord­ing to the “hid­den” meta tag. This can help you (or not), when work­ing with files that can­not have a vis­i­ble META tag for encod­ing in them, and yet, you would not like to open them as UTF-​8

    Hope this helps a bit! :)

Leave a Reply