Before I start explaining how to use filters in PHP 5, it’s convenient to check which of them comes with the PHP distribution. To do this, I’m going to use a pair of handy native functions called “filter_list()” and “filter_id()." The first one will return an array of the existing filters and the second one will retrieve their corresponding IDs. A simple demonstration of how to use these functions is coded below. Take a look at the following example: <?php
// example on using the 'filter_list()' function foreach(filter_list() as $id => $filter) { echo '<p> Filter Name: ' . $filter . ' ------------- Filter ID: ' . filter_id($filter) . '</p><hr />'; }
/* displays the following
Filter Name: int ------------- Filter ID: 257
Filter Name: boolean ------------- Filter ID: 258
Filter Name: float ------------- Filter ID: 259
Filter Name: validate_regexp ------------- Filter ID: 272
Filter Name: validate_url ------------- Filter ID: 273
Filter Name: validate_email ------------- Filter ID: 274
Filter Name: validate_ip ------------- Filter ID: 275
Filter Name: string ------------- Filter ID: 513
Filter Name: stripped ------------- Filter ID: 513
Filter Name: encoded ------------- Filter ID: 514
Filter Name: special_chars ------------- Filter ID: 515
Filter Name: unsafe_raw ------------- Filter ID: 516
Filter Name: email ------------- Filter ID: 517
Filter Name: url ------------- Filter ID: 518
Filter Name: number_int ------------- Filter ID: 519
Filter Name: number_float ------------- Filter ID: 520
Filter Name: magic_quotes ------------- Filter ID: 521
Filter Name: callback ------------- Filter ID: 1024 */ ?> As you can see in the above code sample, the combined use of the “filter_list()” and “filter_id()” functions provides useful information about the filters that are available with the PHP 5 distribution. In this particular case, the name and ID of each filter is displayed on screen via a simple “foreach” construct, which helps you understand each one's functionality. Of course, from the previous example it’s easy to see that there are filters available for checking integers and float numbers, Boolean values and regular expressions, URLs, email and IP addresses, strings and special characters, and so forth. However, since this will be a progressive process, I’m going to discuss each filter in detail in successive tutorials of this series. At this point you should have some idea of how useful PHP 5 filters can really be for validating incoming data. Therefore, it’s time to start an in-depth discussion of the first filter shown by the previous list. It is the one responsible for checking whether a given value is an integer. This brand new PHP 5 filter will be explored in detail in the next segment. Thus, to learn how to work with it, click on the link below and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|