More Books
PHP 5 Unleashed
PHP 5 Unleashed
Table of Contents
Copyright
Lead Author
Contributing Authors
Acknowledgments
We Want to Hear from You!
Reader Services
Introduction
Organization of the Book
Part I. Working with PHP for General Web Development
Chapter 1. Basic PHP Development
How PHP Scripts Work
Basic PHP Syntax
Basic PHP Data Types
Variable Manipulation
Control Structures
User-Defined Functions
Dynamic Variables and Functions
Multiple File PHP Scripts
References
Strings in PHP
Comparing Strings
Advanced String Comparison
Search and Replacement
Formatting Strings
Strings and Locales
Formatting Date and Time Values
Summary
Chapter 2. Arrays
Basic Arrays
Implementing Arrays
More Array Materials
Chapter 3. Regular Expressions
The Basics of Regular Expressions
Limitations of the Basic Syntax
POSIX Regular Expressions
Perl-Compatible Regular Expressions (PCRE)
PCRE Modifiers
A Few Final Words
Chapter 4. Working with Forms in PHP
HTML Forms 101
Working with Form Submissions in PHP
Summary
Chapter 5. Advanced Form Techniques
Data Manipulation and Conversion
Form Data Integrity
Form Processing
Summary
Chapter 6. Persistent Data Using Sessions and Cookies
HTTP Cookies
PHP Sessions
Advanced Sessions
Summary
Chapter 7. Using Templates
The What and Why of Templates
The Smarty Template Engine
Summary
Part II. Advanced Web Development
Chapter 8. PEAR
What Is PEAR?
Getting and Installing PEAR
Using the PEAR Package Manager
Using the PEAR Website
Using PEAR Packages in Applications
Summary
Reference
Chapter 9. XSLT and Other XML Concerns
Relating XML to HTML
Using XSLT to Describe HTML Output Using XML Input
PHP4 and XSLT Using the DOM XML Module
PHP4 and XSLT Using the XSLT Module
PHP5 and XSLT
Accessing XML Data Using SimpleXML
Generating XML Documents Using PHP
Summary
References
Chapter 10. Debugging and Optimizations
Debugging Your PHP Scripts
Optimizing Your PHP Scripts
Summary
Chapter 11. User Authentication
Authenticating Users in PHP
Securing PHP Code
Summary
Chapter 12. Data Encryption
Shared Secret Versus Public Key
Shared Secret Algorithms
Public Key Cryptography
Using Public Keys in PHP
Summary
Chapter 13. Object-Oriented Programming in PHP
Why Objects?
Creating Basic Classes
Advanced Classes
Special Methods
Class Autoloading
Object Serialization
Exceptions
Iterators
Summary
Chapter 14. Error Handling
The PHP Error-Handling Model
What to Do About Errors
The Default Error Handler
Error Suppression
Custom Error Handlers
Causing Errors
Putting It All Together
Summary
Chapter 15. Working with HTML/XHTML Using Tidy
Introduction
Basic Tidy Usage
Tidy Configuration Options
Using the Tidy Parser
Applications of Tidy
Summary
Chapter 16. Writing Email in PHP
The MIME Protocol
Implementing MIME Email in PHP
Summary
Part III. Building Applications in PHP
Chapter 17. Using PHP for Console Scripting
Core CLI Differences
Working with PHP CLI
CLI Tools and Extensions
Summary
Chapter 18. SOAP and PHP
What Are Web Services?
Installation
Creating Web Services
Consuming Web Services
Looking for Web Services
Summary
Chapter 19. Building WAP-Enabled Websites
What Is WAP?
System Requirements
Introduction to WML
Serving WAP Content
Sample Applications
Summary
Part IV. I/O, System Calls, and PHP
Chapter 20. Working with the File System
Working with Files in PHP
File Permissions
File Access Support Functions
Summary
Chapter 21. Network I/O
DNS/Reverse DNS Lookups
Socket Programming
Network Helper Functions
Summary
Chapter 22. Accessing the Underlying OS from PHP
Introduction
Unix-Specific OS Functionality
Platform-Independent System Functions
A Brief Note About Security
Summary
Part V. Working with Data in PHP
Chapter 23. Introduction to Databases
Using the MySQL Client
Basic MySQL Usage
Summary
Chapter 24. Using MySQL with PHP
Performing Queries from PHP
A MySQLi Session Handler
What Is a Custom Session Handler?
Summary
Chapter 25. Using SQLite with PHP
What Makes SQLite Unique?
Basic SQLite Functionality
Working with PHP UDFs in SQLite
Odds and Ends
Summary
Chapter 26. PHP's dba Functions
Preparations and Settings
Creating a File-Based Database
Writing Data
Reading Data
Sample Application
Conclusion
Part VI. Graphical Output with PHP
Chapter 27. Working with Images
Basic Image Creation Using GD
Using the PHP/GD Drawing Functions
Working with Colors and Brushes
Using Fonts and Printing Strings
General Image Manipulation
Other Graphics Functions
Summary
Chapter 28. Printable Document Generation
A Note Regarding the Examples in This Chapter
Generating Dynamic RTF Documents
Generating Dynamic PDF Documents
Related Resources
Part VII. Appendixes
Appendix A. Installing PHP5 and MySQL
Installing PHP5
Installing MySQL and PHP Modules
Installing PEAR
Appendix B. HTTP Reference
What Is HTTP?
PHP Programming Libraries for HTTP Work
Understanding an HTTP Transaction
HTTP Client Methods
What Comes Back: Server Response Codes
HTTP Headers
Encoding
Identifying Clients and Servers
The "Referer"
Fetching Content from an HTTP Source
Media Types
Cookies: Preserving State and a Tasty Treat
Security and Authorization
Client-Side Caching of HTTP Content
Appendix C. Migrating Applications from PHP4 to PHP5
Configuration
Object-Oriented Programming (OOP)
New Behavior of Functions
Further Reading
Appendix D. Good Programming Techniques and Performance Issues
Common Style Mistakes
Common Security Concerns
Style and SecurityLogging
Summary
Appendix E. Resources and Mailing Lists
Relevant Websites
Mailing Lists and Newsgroups
Index
SYMBOL
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z

Advanced String Comparison

It is somewhat difficult to make a computer "understand" strings the way a human being would. A typical example of this problem is spelling mistakes, particularly when you're dealing with names.

Although no solution exists that even approximates the capabilities of the human brain, several algorithms have been developed over the years to provide a way to measure the "similarity" between two strings in shades of gray instead of black and white.

One such example is the soundex algorithm, initially devised as a filing system for use in the U.S. Census at the end of the 1800s. Soundex works by assigning a value to each consonant of the alphabet and then calculating the total value of a word based on its initial and component syllables. The resulting soundex value is represented by the initial letter of the word and the combined value of its syllables.

The soundex algorithm, which is implemented in PHP through the soundex function, can be extremely valuable when searching for names based on their phonetic representations. For example, the word "Tabini" and "Tabani" have the same soundex values:

<?php
    echo soundex ('Tabini');
    echo "\n";
    echo soundex ('Tabani');
    echo "\n";
?>

which returns the following:

T150
T150

As a result, looking for a name becomes much easier even if its exact spelling is unknown.

A better algorithm for comparing two words based on their phonetic representation is metaphone, which was developed in 1990 by Lawrence Philips. The metaphone algorithm works by assigning a phonetic value to combinations of characters based on their typical use in the English language.

PHP provides an implementation of this algorithm through the metaphone function:

<?php

    echo metaphone ('Tabini');
    echo "\n";
    echo metaphone ('Tabani');
    echo "\n";

?>

The preceding script returns the metaphone value "TBN" for both strings.

Comparing Phrases

Other comparison functions deal with entire phrases. For example, the levenshtein() function calculates the "distance" between two phrases, defined as the minimum number of additions, deletions, or replacements to transform a string into another:

<?php
    echo levenshtein ('Tabini', 'Tabani');
    echo "\n";
?>

The preceding script will return U1 because it's necessary to change only the first "i" in Tabini to an "a" to obtain the string 'Tabani'. Although a lower Levenshtein distance generally means a closer similarity between the two parameters, the value returned by this function gives a better idea of the closeness of the two sentences when you compare it to the length of the first parameter:

<?php

    $lev = levenshtein ('Tabini', 'Tabani');
    $per = $lev / strlen ('Tabini') * 100;
    echo "$per\n";

?>

This results in a value that approximates the percentage of distance between the two parameters. The preceding script will return a distance of approximately 16.67%, which can be translated in a similarity of approximately 83.33% between the two strings by subtracting the distance value from one hundred.

Another way to determine the similarity between two strings is provided by the similar_text function, which computes the number of matches between two strings and determines their similarity:

<?php
    $matches = similar_text ('Tabini', 'Tabani', &$per);
    echo "Matches: $matches - Percentage: $per\n";
?>

Interestingly enough, running this script returns the following result:

Matches: 5 - Percentage: 83.333333333333

which is exactly what we had calculated earlier, based on our percentage transformation of the Levenshtein distance.