The functionpreg_quote()inserts a backslash delimiter before every character of special significance to regular expression syntax. These special characters include$^*( ) +={ } [ ] | \\ : < >. Its prototype follows: string preg_quote(string str [, string delimiter]) The optional parameterdelimiter specifies what delimiter is used for the regular expression, causing it to also be escaped by a backslash. Consider an example: <?php This returns the following: -------------------------------------------- Replacing All Occurrences of a Pattern Thepreg_replace()function operates identically toereg_replace(), except that it uses a Perl-based regular expression syntax, replacing all occurrences ofpatternwithreplacement, and returning the modified result. Its prototype follows: mixed preg_replace(mixed pattern, mixed replacement, mixed str [, int limit]) The optional input parameterlimitspecifies how many matches should take place. Failing to setlimitor setting it to-1will result in the replacement of all occurrences. Consider an example: <?php This returns the following: -------------------------------------------- Interestingly, thepatternandreplacementinput parameters can also be arrays. This function will cycle through each element of each array, making replacements as they are found. Consider this example, which could be marketed as a corporate report filter: <?php This returns the following: -------------------------------------------- Creating a Custom Replacement Function In some situations you might wish to replace strings based on a somewhat more complex set of criteria beyond what is provided by PHP’s default capabilities. For instance, consider a situation where you want to scan some text for acronyms such as IRS and insert the complete name directly following the acronym. To do so, you need to create a custom function and then use the functionpreg_replace_callback()to temporarily tie it into the language. Its prototype follows: mixed preg_replace_callback(mixed pattern, callback callback, mixed str Thepatternparameter determines what you’re looking for, while thestrparameter defines the string you’re searching. Thecallbackparameter defines the name of the function to be used for the replacement task. The optional parameterlimitspecifies how many matches should take place. Failing to setlimitor setting it to-1will result in the replacement of all occurrences. In the following example, a function namedacronym()is passed intopreg_replace_callback()and is used to insert the long form of various acronyms into the target string: <?php // This function will add the acronym's long form if (isset($acronyms[$matches[1]])) // The target text // Add the acronyms' long forms to the target text print_r($newtext); ?> This returns the following: -------------------------------------------- Splitting a String into Various Elements Based on a Case-Insensitive Pattern Thepreg_split()function operates exactly likesplit(), except thatpatterncan also be defined in terms of a regular expression. Its prototype follows: array preg_split(string pattern, string string [, int limit [, int flags]]) If the optional input parameterlimitis specified, onlylimitnumber of substrings are returned. Consider an example: <?php This returns the following: -------------------------------------------- Note Later in this chapter, the section titled “Alternatives for Regular Expression Functions” offers several standard functions that can be used in lieu of regular expressions for certain tasks. In many cases, these alternative functions actually perform much faster than their regular expression counterparts.
blog comments powered by Disqus |
|
|
|
|
|
|
|