Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
PHP Cookbook
Preface
Who This Book Is For
What Is in This Book
Other Resources
Conventions Used in This Book
Comments and Questions
Acknowledgments
1. Strings
Introduction
Accessing Substrings
Extracting Substrings
Replacing Substrings
Processing a String One Byte at a Time
Reversing a String by Word or Byte
Generating a Random String
Expanding and Compressing Tabs
Controlling Case
Interpolating Functions and Expressions Within Strings
Trimming Blanks from a String
Generating Comma-Separated Data
Parsing Comma-Separated Data
Generating Fixed-Width Field Data Records
Parsing Fixed-Width Field Data Records
Taking Strings Apart
Wrapping Text at a Certain Line Length
Storing Binary Data in Strings
Program: Downloadable CSV File
2. Numbers
Introduction
Checking Whether a Variable Contains a Valid Number
Comparing Floating-Point Numbers
Rounding Floating-Point Numbers
Operating on a Series of Integers
Generating Random Numbers Within a Range
Generating Predictable Random Numbers
Generating Biased Random Numbers
Taking Logarithms
Calculating Exponents
Formatting Numbers
Formatting Monetary Values
Printing Correct Plurals
Calculating Trigonometric Functions
Doing Trigonometry in Degrees, Not Radians
Handling Very Large or Very Small Numbers
Converting Between Bases
Calculating Using Numbers in Bases Other Than Decimal
Finding the Distance Between Two Places
3. Dates and Times
Introduction
Finding the Current Date and Time
Converting Time and Date Parts to an Epoch Timestamp
Converting an Epoch Timestamp to Time and Date Parts
Printing a Date or Time in a Specified Format
Finding the Difference of Two Dates
Finding the Day in a Week, Month, or Year
Validating a Date
Parsing Dates and Times from Strings
Adding to or Subtracting from a Date
Calculating Time with Time Zones and Daylight Saving Time
Generating a High-Precision Time
Generating Time Ranges
Using Non-Gregorian Calendars
Program: Calendar
4. Arrays
Introduction
Specifying an Array Not Beginning at Element 0
Storing Multiple Elements per Key in an Array
Initializing an Array to a Range of Integers
Iterating Through an Array
Deleting Elements from an Array
Changing Array Size
Appending One Array to Another
Turning an Array into a String
Printing an Array with Commas
Checking if a Key Is in an Array
Checking if an Element Is in an Array
Finding the Position of a Value in an Array
Finding Elements That Pass a Certain Test
Finding the Largest or Smallest Valued Element in an Array
Reversing an Array
Sorting an Array
Sorting an Array by a Computable Field
Sorting Multiple Arrays
Sorting an Array Using a Method Instead of a Function
Randomizing an Array
Removing Duplicate Elements from an Array
Applying a Function to Each Element in an Array
Finding the Union, Intersection, or Difference of Two Arrays
Iterating Efficiently over Large or Expensive Datasets
Accessing an Object Using Array Syntax
5. Variables
Introduction
Avoiding == Versus = Confusion
Establishing a Default Value
Exchanging Values Without Using Temporary Variables
Creating a Dynamic Variable Name
Persisting a Local Variable’s Value Across Function Invocations
Sharing Variables Between Processes
Encapsulating Complex Data Types in a String
Dumping Variable Contents as Strings
6. Functions
Introduction
Accessing Function Parameters
Setting Default Values for Function Parameters
Passing Values by Reference
Using Named Parameters
Enforcing Types of Function Arguments
Creating Functions That Take a Variable Number of Arguments
Returning Values by Reference
Returning More Than One Value
Skipping Selected Return Values
Returning Failure
Calling Variable Functions
Accessing a Global Variable Inside a Function
Creating Dynamic Functions
7. Classes and Objects
Introduction
Instantiating Objects
Defining Object Constructors
Defining Object Destructors
Implementing Access Control
Preventing Changes to Classes and Methods
Defining Object Stringification
Requiring Multiple Classes to Behave Similarly
Creating Abstract Base Classes
Assigning Object References
Cloning Objects
Overriding Property Accesses
Calling Methods on an Object Returned by Another Method
Aggregating Objects
Accessing Overridden Methods
Creating Methods Dynamically
Using Method Polymorphism
Defining Class Constants
Defining Static Properties and Methods
Controlling Object Serialization
Introspecting Objects
Checking If an Object Is an Instance of a Specific Class
Autoloading Class Files upon Object Instantiation
Instantiating an Object Dynamically
Program: whereis
8. Web Fundamentals
Introduction
Setting Cookies
Reading Cookie Values
Deleting Cookies
Building a Query String
Reading the POST Request Body
Using HTTP Basic or Digest Authentication
Using Cookie Authentication
Reading an HTTP Header
Writing an HTTP Header
Sending a Specific HTTP Status Code
Redirecting to a Different Location
Flushing Output to the Browser
Buffering Output to the Browser
Compressing Web Output
Reading Environment Variables
Setting Environment Variables
Communicating Within Apache
Redirecting Mobile Browsers to a Mobile Optimized Site
Program: Website Account (De)activator
Program: Tiny Wiki
Program: HTTP Range
9. Forms
Introduction
Processing Form Input
Validating Form Input: Required Fields
Validating Form Input: Numbers
Validating Form Input: Email Addresses
Validating Form Input: Drop-Down Menus
Validating Form Input: Radio Buttons
Validating Form Input: Checkboxes
Validating Form Input: Dates and Times
Validating Form Input: Credit Cards
Preventing Cross-Site Scripting
Processing Uploaded Files
Working with Multipage Forms
Redisplaying Forms with Inline Error Messages
Guarding Against Multiple Submissions of the Same Form
Preventing Global Variable Injection
Handling Remote Variables with Periods in Their Names
Using Form Elements with Multiple Options
Creating Drop-Down Menus Based on the Current Date
10. Database Access
Introduction
Using DBM Databases
Using an SQLite Database
Connecting to an SQL Database
Querying an SQL Database
Retrieving Rows Without a Loop
Modifying Data in an SQL Database
Repeating Queries Efficiently
Finding the Number of Rows Returned by a Query
Escaping Quotes
Logging Debugging Information and Errors
Creating Unique Identifiers
Building Queries Programmatically
Making Paginated Links for a Series of Records
Caching Queries and Results
Accessing a Database Connection Anywhere in Your Program
Program: Storing a Threaded Message Board
Using Redis
11. Sessions and Data Persistence
Introduction
Using Session Tracking
Preventing Session Hijacking
Preventing Session Fixation
Storing Sessons in Memcached
Storing Sessions in a Database
Storing Arbitrary Data in Shared Memory
Caching Calculated Results in Summary Tables
12. XML
Introduction
Generating XML as a String
Generating XML with DOM
Parsing Basic XML Documents
Parsing Complex XML Documents
Parsing Large XML Documents
Extracting Information Using XPath
Transforming XML with XSLT
Setting XSLT Parameters from PHP
Calling PHP Functions from XSLT Stylesheets
Validating XML Documents
Handling Content Encoding
Reading RSS and Atom Feeds
Writing RSS Feeds
Writing Atom Feeds
13. Web Automation
Introduction
Marking Up a Web Page
Cleaning Up Broken or Nonstandard HTML
Extracting Links from an HTML File
Converting Plain Text to HTML
Converting HTML to Plain Text
Removing HTML and PHP Tags
Responding to an Ajax Request
Integrating with JavaScript
Program: Finding Stale Links
Program: Finding Fresh Links
14. Consuming RESTful APIs
Introduction
Fetching a URL with the GET Method
Fetching a URL with the POST Method and Form Data
Fetching a URL with an Arbitrary Method and POST Body
Fetching a URL with Cookies
Fetching a URL with Arbitrary Headers
Fetching a URL with a Timeout
Fetching an HTTPS URL
Debugging the Raw HTTP Exchange
Making an OAuth 1.0 Request
Making an OAuth 2.0 Request
15. Serving RESTful APIs
Introduction
Exposing and Routing to a Resource
Exposing Clean Resource Paths
Exposing a Resource for Reading
Creating a Resource
Editing a Resource
Deleting a Resource
Indicating Errors and Failures
Supporting Multiple Formats
16. Internet Services
Introduction
Sending Mail
Sending MIME Mail
Reading Mail with IMAP or POP3
Getting and Putting Files with FTP
Looking Up Addresses with LDAP
Using LDAP for User Authentication
Performing DNS Lookups
Checking If a Host Is Alive
Getting Information About a Domain Name
17. Graphics
Introduction
Drawing Lines, Rectangles, and Polygons
Drawing Arcs, Ellipses, and Circles
Drawing with Patterned Lines
Drawing Text
Drawing Centered Text
Building Dynamic Images
Getting and Setting a Transparent Color
Overlaying Watermarks
Creating Thumbnail Images
Reading EXIF Data
Serving Images Securely
Program: Generating Bar Charts from Poll Results
18. Security and Encryption
Introduction
Preventing Session Fixation
Protecting Against Form Spoofing
Ensuring Input Is Filtered
Avoiding Cross-Site Scripting
Eliminating SQL Injection
Keeping Passwords Out of Your Site Files
Storing Passwords
Dealing with Lost Passwords
Verifying Data with Hashes
Encrypting and Decrypting Data
Storing Encrypted Data in a File or Database
Sharing Encrypted Data with Another Website
Detecting SSL
Encrypting Email with GPG
19. Internationalization and Localization
Introduction
Determining the User’s Locale
Localizing Text Messages
Localizing Dates and Times
Localizing Numbers
Localizing Currency Values
Localizing Images
Localizing Included Files
Sorting in a Locale-Aware Order
Managing Localization Resources
Setting the Character Encoding of Outgoing Data
Setting the Character Encoding of Incoming Data
Manipulating UTF-8 Text
20. Error Handling
Introduction
Finding and Fixing Parse Errors
Creating Your Own Exception Classes
Printing a Stack Trace
Reading Configuration Variables
Setting Configuration Variables
Hiding Error Messages from Users
Tuning Error Handling
Using a Custom Error Handler
Logging Errors
Eliminating “headers already sent” Errors
Logging Debugging Information
21. Software Engineering
Introduction
Using a Debugger Extension
Writing a Unit Test
Writing a Unit Test Suite
Applying a Unit Test to a Web Page
Setting Up a Test Environment
Using the Built-in Web Server
22. Performance Tuning
Introduction
Using an Accelerator
Timing Function Execution
Timing Program Execution by Function
Timing Program Execution by Statement
Timing Program Execution by Section
Profiling with a Debugger Extension
Stress-Testing Your Website
Avoiding Regular Expressions
23. Regular Expressions
Introduction
Switching from ereg to preg
Matching Words
Finding the nth Occurrence of a Match
Choosing Greedy or Nongreedy Matches
Finding All Lines in a File That Match a Pattern
Capturing Text Inside HTML Tags
Preventing Parentheses from Capturing Text
Escaping Special Characters in a Regular Expression
Reading Records with a Pattern Separator
Using a PHP Function in a Regular Expression
24. Files
Introduction
Creating or Opening a Local File
Creating a Temporary File
Opening a Remote File
Reading from Standard Input
Reading a File into a String
Counting Lines, Paragraphs, or Records in a File
Processing Every Word in a File
Picking a Random Line from a File
Randomizing All Lines in a File
Processing Variable-Length Text Fields
Reading Configuration Files
Modifying a File in Place Without a Temporary File
Flushing Output to a File
Writing to Standard Output
Writing to Many Filehandles Simultaneously
Escaping Shell Metacharacters
Passing Input to a Program
Reading Standard Output from a Program
Reading Standard Error from a Program
Locking a File
Reading and Writing Custom File Types
Reading and Writing Compressed Files
25. Directories
Introduction
Getting and Setting File Timestamps
Getting File Information
Changing File Permissions or Ownership
Splitting a Filename into Its Component Parts
Deleting a File
Copying or Moving a File
Processing All Files in a Directory
Getting a List of Filenames Matching a Pattern
Processing All Files in a Directory Recursively
Making New Directories
Removing a Directory and Its Contents
Program: Web Server Directory Listing
Program: Site Search
26. Command-Line PHP
Introduction
Parsing Program Arguments
Parsing Program Arguments with getopt
Reading from the Keyboard
Running PHP Code on Every Line of an Input File
Reading Passwords
Colorizing Console Output
Program: DOM Explorer
27. Packages
Introduction
Defining and Installing Composer Dependencies
Finding Composer Packages
Installing Composer Packages
Using the PEAR Installer
Finding PEAR Packages
Finding Information About a Package
Installing PEAR Packages
Upgrading PEAR Packages
Uninstalling PEAR Packages
Installing PECL Packages
Index
Colophon
Copyright
← Prev
Back
Next →
← Prev
Back
Next →