25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Strings And Patterns ” 89<br />

Parsing Formatted Input<br />

The sscanf() family of functions works in a similar way to printf(), except that, instead<br />

of formatting output, it allows you to parse formatted input. For example, consider<br />

the following:<br />

$data = ’123 456 789’;<br />

$format = ’%d %d %d’;<br />

var_dump (sscanf ($data, $format));<br />

When this code is executed, the function interprets its input according to the rules<br />

specified in the format string and returns an array that contains the parsed data:<br />

array(3) {<br />

[0]=><br />

int(123)<br />

[1]=><br />

int(456)<br />

[2]=><br />

int(789)<br />

}<br />

Note that the data must match the format passed to sscanf() exactly—or the function<br />

will fail to retrieve all the values. For this reason, sscanf() is normally only useful<br />

in those situations in which input follows a well-defined format (that is, it is not provided<br />

by the user!).<br />

Perl-compatible Regular Expressions<br />

Perl Compatible Regular Expressions (normally abbreviated as “PCRE”) offer a very<br />

powerful string-matching and replacement mechanism that far surpasses anything<br />

we have examined so far.<br />

Regular expressions are often thought of as very complex—and they can be at<br />

times. However, properly used they are relatively simple to understand and fairly<br />

easy to use. Given their complexity, of course, they are also much more computationally<br />

intensive than the simple search-and-replace functions we examined ear-<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!