11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

CHAPTER 9 • STRINGS AND REGULAR EXPRESSIONSwww.it-ebooks.infoinnumerable online and print tutorials have been written regarding this matter, I’ll focus on providingyou with just a basic introduction to the topic. If you are already well-acquainted with regular expressionsyntax, feel free to skip past the tutorial to the “<strong>PHP</strong>’s Regular Expression Functions (Perl Compatible)”section.■ Caution As I stated in the chapter introduction, <strong>PHP</strong>’s POSIX-based regular expression library has beendeprecated as of version 5.3.0. Attempting to use any of this library’s functions will result in the generation of anotice of level E_DEPRECATED. Because this is a relatively recent development, I have decided to retain thefollowing two POSIX-specific sections in this edition as you may need to refactor legacy code to use the solesupported Perl library and therefore will need to understand POSIX-specific syntax. However, do not use thesefunctions in new code, as they will eventually be removed from the language altogether!Regular Expression Syntax (POSIX)The structure of a POSIX regular expression is similar to that of a typical arithmetic expression: variouselements (operators) are combined to form a more complex expression. The meaning of the combinedregular expression elements is what makes them so powerful. You can use the syntax to find not onlyliteral expressions, such as a specific word or number, but also a multitude of semantically different butsyntactically similar strings, such as all HTML tags in a file.■ Note POSIX stands for Portable Operating System Interface for Unix and is representative of a set of standardsoriginally intended for Unix-based operating systems. POSIX regular expression syntax is an attempt to standardizehow regular expressions are implemented in many programming languages.The simplest regular expression is one that matches a single character, such as g, to strings such asgog, haggle, and bag. You could combine several letters together to form larger expressions, such as gan,which logically would match any string containing gan: gang, organize, or Reagan, for example.You can also test for several different expressions simultaneously by using the pipe (|) character. Forexample, you could test for php or zend via the regular expression php|zend.Before getting into <strong>PHP</strong>’s POSIX-based regular expression functions, let’s review three methods thatPOSIX supports for locating different character sequences: brackets, quantifiers, and predefinedcharacter ranges.BracketsBrackets ([]) are used to represent a list, or range, of characters to be matched. For instance, contrary tothe regular expression php, which will locate strings containing the explicit string php, the regularexpression [php] will find any string containing the character p or h. Several commonly used characterranges follow:192

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

Saved successfully!

Ooh no, something went wrong!