PURVEYOR OF FINE WORDS

May 24, 2006

May 24 2006

How to create PHP extensions (aka .so objects)

Although PHP has a great library of functions, many of them are not included in the standard build, or haven’t been included into the popular package installers like yum or apt-get. The man page doesn’t leave you with much instruction, other than something like “compile PHP with the flag –with-pspell[=dir]”. At this point you have 2 options:

  • Recompile PHP
    Find your existing PHP configure command and append this new flag, and recompile PHP. This takes a while and is generally quite bothersome, if not unacceptable like when you’re in a production environment.
  • Create a dynamic extension
    Compile a separate file (usually ending in .so) that you copy into a PHP directory, and edit php.ini. If you are running multiple machines on the same OS, you can just copy the file to all those machines as well. Much easier, and you can turn it on and off at will.

Here’s how to create the extension for modules that appear in the PHP manual on a linux-based system (for third-party extensions, it’s most likely the same).

  1. Check that you have the PHP development package, which often comes in a separate package. Yum lists it as php-devel. You’ll need its components in a few steps.
  2. Download the PHP source code for whatever version you’re currently running
  3. In the source code, there is an ext/ directory that should contain a subdirectory for the module that you’re looking for. Change to that subdirectory, i.e. ext/pspell/.
  4. Type phpize
    (This won’t work if you didn’t verify step 1)
  5. Type ./configure –with-pspell=/usr
    Replace the red portion with the text that is specified in the PHP man page for the module you want. For example, MySQL improved would be something like --with-mysqli=/usr/local/mysql/bin/mysql_config. Be aware that the path is sometimes a base directory, and sometimes needs to point to a specific file. Read the PHP docs carefully.
  6. Type make
  7. When finished, the compiler should tell you where it created the .so file (most likely in the modules/ subdirectory of your current location). Copy the .so file to your PHP extensions directory, i.e. /usr/lib/php/modules. If you don’t know this, it’s listed in your php.ini file under the extension_dir parameter. You’ll need root access to do this.
  8. Finally, tell PHP about your new extension by adding one line to php.ini:
    extension=pspell.so
    Alternatively, if you already have a bunch of extensions installed, you can place it in your /etc/php.d directory in it’s own ini file for a cleaner installation approach.
  9. Restart apache, if you’re using it
  10. Check phpinfo() to verify that your new module is installed

3 editorials

May 24 2006

Ajax spell check as you type

I’ve been looking for a nice web Javascript spell checker, and came across a great implementation by Emil that he named LiteSpellChecker. It mimics the spell checker in MS Word by underlining misspelled words and presenting a nice substitute word selection menu. The javascript takes a standard <textarea> element, erases the background, and inserts a shadow <div> underneath that holds the redline segments.
spell check screenshot in Firefox OS X

Bug Fixes (based on 2005-7-24 version of LiteSpellChecker)

The current implementation on his site has some bugs, so I’ve started to tackle some of them:

  • Fixed: Redlines get misaligned when scrolling with arrow keys in Firefox
    When scrolling long text using Firefox, the redlines become misaligned as you move towards the bottom of the page. Once they begin to misalign, you have to manually refresh in order get it right again.
  • Fixed: Ignore word function breaks if there are non-word segments in the text
    If the text contains characters like ++ or @@, then the ignore words function fails. This problem crops up whenever you have wiki markup syntax.
  • Fixed: Numbers are flagged as misspelled
    Any word that contains a number is now ignored by the spell checker.

Performance Improvements

Since many of my users will be working with long documents, performance is key.

  • Sped up ignore function by almost 5x
    The ignore word function loops through all words and feeds it back into the spellchecker. In long documents, the lag becomes noticable, and also causes a flicker of all the redlines. Instead of blindly looping through all words, I changed it to a case-insensitive search for the ignored word, and only processed the matching words. Venkman reports a speed up from 278.2ms to 57.95ms on a Powerbook G4.
  • Improved responsiveness while editing long text
    Since the spell checker updates the spelling status on every keystroke, the UI becomes unusable when editing long text. I added a spell check delay that suspends the spell checker while typing continuously. You can adjust the sensitivity via the SPELL_CHECK_DELAY variable.

Download my source code. Important: read Emil’s demo page before attempting to do anything with these files.

5 editorials