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

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');
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');
07 $ff = new File_Find();
08 $files = $ff->search("/.*\.php$/","scripts/","perl");
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 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');
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 PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

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 News Articles


PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre x360

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga 910

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?