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 #87. Build GUI Interfaces with GTk

Use GTk to build cross-platform GUIs for your PHP code.

Why limit yourself to web markup, or even tag-based devices? Why not take over the desktop itself with your PHP code? With the GTk toolkit you can do that, and it's really easy. This hack shows how to use GTk to develop a simple regular-expression test application.

The Code

Save the code in Example 9-6 as retest.php.

Example 9-6. A GTk regular expression tester

<?php
if( !extension_loaded('gtk')) {
	dl( 'php_gtk.'.PHP_SHLIB_SUFFIX); } $start_regex = "/name:\\s*(.*?)\\n/"; $start_text = "name: Jack\nname:Lori\nname:Megan\n"; function delete_event() { return false; } function shutdown() { gtk::main_quit(); } function run() { global $rb_regex, $tb_text, $ft; $regex = $rb_regex->get_chars(0, -1); $text = $tb_text->get_chars(0, -1); preg_match_all( $regex, $text, $found ); $ft->clear(); $i = 0; foreach( $found[1] as $f ) { $ft->insert( $i, array( $f ) ); $i++; } } $window = &new GtkWindow(); $window->set_usize( 700, 400 ); $window->set_title( "Regular Expression Tester" ); $window->connect('destroy', 'shutdown'); $window->connect('delete-event', 'delete_event'); $bb = new GtkTable(); $rb = new GtkTable(); $rb_label = new GtkLabel( "Regex:" ); $rb->attach( $rb_label, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 5, 5 ); $rb_regex = new GtkEntry(); $rb_regex->insert_text( $start_regex, 0 ); $rb->attach( $rb_regex, 1, 2, 0, 1, GTK_FILL, GTK_SHRINK, 5, 5 ); $rb_run = new GtkButton( "Run" ); $rb_run->connect('clicked', ...

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