O'Reilly logo

PHP Hacks by Jack D. Herrington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #99. Put Wikipedia on Your PSP

Use MySQL and PHP to build a dictionary from Wikipedia that fits in your hip pocket.

Wikipedia (http://www.wikipedia.org) is probably the single most informative site on the Internet. It's a user-contributed encyclopedia and dictionary. What's even better is that you can download the entire contents of Wikipedia and use it for your own purposes.

In my case, I wanted the Wikipedia dictionary on my PSP. Being a PHP hacker, of course I had to use PHP and MySQL; I created a set of static pages from Wikipedia and then downloaded those pages to my PSP memory stick. It's not dynamic, but it still impresses my buddies when I can look up grok on my PSP.

Figure 10-15 shows the basic flow of the processing in this hack. The Wiktionary contents are loaded into the MySQL database [Hack #1] . An elaborate dict.php script takes the contents of the database and creates a set of specially formatted HTML pages appropriate to the PSP.

The processing flow of the PSP dictionary creator

Figure 10-15. The processing flow of the PSP dictionary creator

The Code

Save the code in Example 10-10 as dict.php.

Example 10-10. Downloading the current Wikipedia to create static HTML

<?php
require_once( "DB.php" );
require_once( "Text/Wiki.php" );

$g_wiki = new Text_Wiki();

$g_wiki->enableRule('html');
$g_wiki->enableRule('list');function wikiToHTML( $text ) { global $g_wiki; $text = preg_replace( "/\=\=\=\s* Pronunciation.*?\n\=\=\=/is", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required