Using PEAR

Using PEAR: Part II

In the previous page we looked at managing and installing PHP Extension and Application Repository (PEAR) packages, as well as how to use such a package from a PHP script. Now we will look at some of the other packages in the PEAR library.

The packages required for this column are available from www.pear.php.net.

To install them into your local PHP system, copy all packages from the PEAR site into a temporary directory, then type:

$ pear install

Where <package filename> is the filename of the package you are installing. The packages are stored in tar gzipped format, so you may get an error like this:

The extension 'zlib' couldn't be found.

Please make sure your version of PHP was built with 'zlib' support.

If so, you will need to rebuild PHP passing the '--with-zlib' argument to configure (see the INSTALL file in the PHP source archive on the cover CD for more information).

File system packages

It is often convenient to store information for your Web application on the file system. Using the File_Find PEAR package you can search and map directories used by your application. Consider the following simple script, which lists all files ending with .php in a specified directory:

01 <?
02 require_once('PEAR.php');
03 require_once('File.php');
04 require_once('File/Find.php');
05
06 $ff = new File_Find();
07 $files = $ff->glob("/.*\.php$/","scripts/","perl");
08 ?>

Lines 02, 03 and 04 include the relevant PEAR package files. Line 07 creates a new File_Find object. Line 08 calls the glob() method with three arguments: the first a search pattern, the second the directory to search - in this case, the directory scripts/ - and the third the search algorithm to use. The search pattern may look strange to readers unfamiliar with Perl regular expression syntax. It means: match strings ending in '.php'. For more information on Perl regular expressions see the Regular Expression Functions (Perl Compatible) section of the PHP manual.

This script can easily be expanded to search all sub-directories by calling the search method.

$files = $ff->search("/.*\.php$/","scripts/","perl");

Alternatively, it might be useful to build a map of a directory hierarchy and its files. This can be done by calling the maptree() method:

$files = $ff->maptree("scripts/");

This returns a two dimensional array. The first element, $files[0], is an array of sub-directories; the second element, $files[1], is an array of files in those sub-directories.

Search and replace

PEAR makes it easy to search multiple files and replace strings of text. Consider the following script, which changes a copyright string in each file.

01 <?
02 require_once('PEAR.php');
03 require_once('File.php');
04 require_once('File/Find.php');
05 require_once('File/SearchReplace.php');
06
07 $ff = new File_Find();
08 $files = $ff->search("/.*\.php$/","scripts/","perl");
09
10 $fsr = new File_SearchReplace("Copyright Abc, Inc. 2002","Copyright Abc, Inc. 2003", $files, "scripts/");
11 $fsr->doSearch();
12 ?>

This script builds on the file system search functionality we looked at above by building a list of files that we want to manipulate (line 08). Line 10 creates a File_SearchReplace object. We pass four arguments to the object constructor: the first argument is the string to replace, the second argument is the replacement string, the third argument is an array of files to search and replace in, and the fourth argument specifies the directory in which to look.

Line 11 performances the actual search and replace.

Logging

Logging can be a very important part of application development, debugging and management. The PEAR Log package provides a convenient mechanism for doing this in a standardised manner.

01 <?
02 require_once('Log.php');
03
04 $log = Log::singleton("file","log.txt");
05 $log->log("Log file test");
06 $log->WriteOut();
07 ?>

Line 04 creates an instance of the object we're interested in: namely, a file-based logging object (argument one) that logs to log.txt (argument two). Line 05 buffers a log message inside the object and line 06 writes the buffer to log.txt. This last point is very important: if you do not call the WriteOut() method the log message(s) will be lost.

Join the newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Kurt Hegetschweiler

Brother PocketJet PJ-773 A4 Portable Thermal Printer

It’s perfect for mobile workers. Just take it out — it’s small enough to sit anywhere — turn it on, load a sheet of paper, and start printing.

Matthew Stivala

HP OfficeJet 250 Mobile Printer

The HP OfficeJet 250 Mobile Printer is a great device that fits perfectly into my fast paced and mobile lifestyle. My first impression of the printer itself was how incredibly compact and sleek the device was.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?