Sub-Domains on Windows localhost

General 2 Comments

I find it useful when working on a web project to set up a sub-domain on my development PC's "localhost" Apache installation. This allows me to test the project by accessing pages via that sub-domain, treating the directory being used as the root of that sub-domain as if it were the web document root directory (i.e.: $_SERVER['DOCUMENT_ROOT'] in your PHP scripts will point to that directory when the page is launched via that sub-domain).

Setting up a localhost sub-domain in an Windows/Apache environment is fairly easy. You just need to set up a few lines of text in three files, then restart Apache. First, make sure that your Apache httpd.conf file has the following in it, adjusting the path to the "httpd-vhosts.conf" file as appropriate for your installation:

Read the rest...

Filtering MS Word Text

HTML, PHP No Comments

A common annoyance when dealing with user-supplied content is the way MS Word uses some non-standard character encodings (at least non-standard in terms of the web). Among others, these include the directional (a.k.a. "smart") quotes. The problem occurs when you output text that contains those characters as a result of a user copying and pasting text from a Word document. Typically they are not interpreted by the browser and the font being used, resulting in the dreaded place-holder characters (question marks, boxes, etc.).

If outputting the UTF-8 character encoding in your PHP pages, I came up with the following PHP function to help deal with this. It is inspired by this comment on Chris Shiflett's blog. It is simply a use of the str_replace() function to replace some known problem characters with character entities that should work better when outputting UTF-8 content.

<?php
function filterText($text)
{
   $search = array (
      '&',
      '<',
      '>',
      '"',
      chr(212),
      chr(213),
      chr(210),
      chr(211),
      chr(209),
      chr(208),
      chr(201),
      chr(145),
      chr(146),
      chr(147),
      chr(148),
      chr(151),
      chr(150),
      chr(133)
   );
   $replace = array (
      '&amp;',
      '&lt;',
      '&gt;',
      '&quot;',
      '&#8216;',
      '&#8217;',
      '&#8220;',
      '&#8221;',
      '&#8211;',
      '&#8212;',
      '&#8230;',
      '&#8216;',
      '&#8217;',
      '&#8220;',
      '&#8221;',
      '&#8211;',
      '&#8212;',
      '&#8230;'
   );
   return str_replace($search, $replace, $text);
}

// USAGE:
header('Content-Type: text/html; charset="UTF-8"');
echo filterText($test);
?>

UTF8 in PHP and MySQL

PHP 3 Comments

The intent of this article is to tie together some things I've learned to do in order to get my web apps to "play nicely" with the UTF8 character set. Before we go any further, let me state that I do not claim to be an expert on this; the following is simply a collection of things I've discovered here and there on the web, and which together seem to help smooth out most of the bumps in the road of using UTF8.

So let's start with the database itself. To get your varchar and text fields talking UTF8, you should assign both the character set and a corresponding collation. (See the MySQL manual section on character sets and collations to see the differences between the various collation types.) You can assign this stuff at the field level should you desire, but generally I just assign it at the table level:

Read the rest...