PHP Simple HTML DOM Parser


I discovered this little beauty awhile ago, but used it once then forgot about it. I rediscovered it today while working on an awesome WordPress theme framework that I’m developing. However I ran into some issues with it and wanted to document them here for future reference, and to help others avoid the debugging grief I just went through.

Accessing Hyphenated Attributes

The documentation states that to access the attribute of an element, use the arrow operator. This works fine in the example:

// Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
$value = $e->href;

However it doesn’t work so well when the attribute we’re trying to access is data-role or data-inset, for obvious reasons. Turns out there’s an attr[] array that stores these values as well, and is much friendlier for these types of attributes. So to access the data-role attribute we’d just use:

$value = $e->attr['data-role'];

Bug When Adding Attributes

It seems that when we add an attribute that didn’t already exist on the element using the aforementioned attr[] array, it fails to add a leading space. We end up with <uldata-role="listview"data-inset="true">, which isn’t really what we’re looking for. The fix is quite easy. Just add the leading space when you assign the attribute, though doing some sanity checks doesn’t hurt:

if ( ! $ul->attr['data-role'] )
	// Add leading space on attribute name
	$ul->attr[' data-role'] = 'listview';
	$ul->attr['data-role'] = 'listview';

That’s all I’ve discovered so far.  I do highly recommend this helper class as it lets you manipulate the DOM quite well. I’m normally not opposed to JavaScript, but most of the things I do these days tend to be fairly JS-heavy, so getting some of this lifting done on the server (and caching it) will definitely help with page load times.

PHP Simple HTML DOM Parser on Sourceforge

Overriding the Default Search Form

WordPress LogoI’m working on building an HTML5 WordPress theme that I’ll use as a foundation for all my projects. I’m sort of taking the shoot-first, ask-questions-later approach. That is, I’m using as much HTML5 goodness as I can, and I’ll deal with backwards compatibility later. So, I wanted to use a proper HTML5 search input.

In order to override the default search form, all you have to do is create a file called searchform.php in the root of your theme’s directory. WordPress will pick this up and use it. For reference, my form looks like this:

<form role="search" method="get" id="searchform" action="<?php bloginfo( 'wpurl' ); ?>">
<input type="search" value="" name="s" id="s" results="5" autosave="gg_search" placeholder="Search..." />
<input type="submit" id="searchsubmit" value="Search" />

audio thai Released!

audio thaiaudio thai has finally been released!

From the audio thai website:

How can audio thai help me learn Thai?

Phonetic systems don’t really work when learning Thai as many sounds in the Thai language don’t exist in English.

audio thai has been designed to allow you to learn Thai, increase your vocabulary and improve your pronunciation by listening to audio files of native Thai speakers.

Only by listening to the audio can you fully understand and learn the true sound, length and tone of the word or syllable.

audio thai is intuitive and easy to use!

  • Over 10,000 words and sentences and growing . . .
  • Full Thai audio playback for every record.
  • Open multiple pop-up windows + multiple copies of audio thai.
  • Create and manage your own favourites library.
  • Get you started lesson plans.
  • A comprehensive categories and classifiers menu.
  • Grammar simply explained with examples + alphabet.
  • Graphical arrows to help reinforce the correct tone.
  • User management and inappropriate content filter for minors.
  • Free minor updates.

Try out audio thai for free!