PHP offers seven functions for searching strings using POSIX-style regular expressions: ereg(), ereg_replace(), eregi(), eregi_replace(), split(), spliti(), and sql_regcase(). These functions are discussed in this section. Performing a Case-Sensitive Search Theereg()function executes a case-sensitive search of a string for a defined pattern, returningTRUEif the pattern is found, andFALSEotherwise. Its prototype follows: boolean ereg(string pattern, string string [, array regs]) Here’s how you could useereg()to ensure that a username consists solely of lowercase letters: <?php In this case,ereg()will returnTRUE, causing the error message to output. The optional input parameterregscontains an array of all matched expressions that are grouped by parentheses in the regular expression. Making use of this array, you could segment a URL into several pieces, as shown here: <?php // Break $url down into three distinct pieces: echo $regs[0]; // outputs the entire string http://www.apress.com This returns the following: -------------------------------------------- Performing a Case-Insensitive Search Theeregi()function searches a string for a defined pattern in a case-insensitive fashion. Its prototype follows: int eregi(string pattern, string string, [array regs]) This function can be useful when checking the validity of strings, such as passwords. This concept is illustrated in the following example: <?php In this example, the user must provide an alphanumeric password consisting of eight to ten characters, or else an error message is displayed. Replacing Text in a Case-Sensitive Fashion Theereg_replace()function operates much likeereg(), except that its power is extended to finding and replacing a pattern with a replacement string instead of simply locating it. Its prototype follows: string ereg_replace(string pattern, string replacement, string string) If no matches are found, the string will remain unchanged. Likeereg(),ereg_replace()is case sensitive. Consider an example: <?php This returns the following: -------------------------------------------- A rather interesting feature of PHP’s string-replacement capability is the ability to back-reference parenthesized substrings. This works much like the optional input parameterregsin the functionereg(), except that the substrings are referenced using backslashes, such as\0,\1,\2, and so on, where\0refers to the entire string,\1the first successful match, and so on. Up to nine back references can be used. This example shows how to replace all references to a URL with a working hyperlink: $url = "Apress (http://www.apress.com)"; $url = ereg_replace("http://([a-zA-Z0-9./-]+)([a-zA-Z/]+)", Note Althoughereg_replace()works just fine, another predefined function namedstr_replace()is actually much faster when complex regular expressions are not required.str_replace()is discussed in the later section “Replacing All Instances of a String with Another String.” Replacing Text in a Case-Insensitive Fashion Theeregi_replace()function operates exactly likeereg_replace(), except that the search for string eregi_replace(string pattern, string replacement, string string) Splitting a String into Various Elements Based on a Case-Sensitive Pattern Thesplit()function divides a string into various elements, with the boundaries of each element based on the occurrence of a defined pattern within the string. Its prototype follows: array split(string pattern, string string [, int limit]) The optional input parameterlimitis used to specify the number of elements into which the string should be divided, starting from the left end of the string and working rightward. In cases where the pattern is an alphabetical character,split()is case sensitive. Here’s how you would usesplit()to break a string into pieces based on occurrences of horizontal tabs and newline characters: <?php This returns the following: -------------------------------------------- Splitting a String into Various Elements Based on a Case-Insensitive Pattern Thespliti()function operates exactly in the same manner as its sibling,split(), except that its pattern is treated in a case-insensitive fashion. Its prototype follows: array spliti(string pattern, string string [, int limit]) Accomodating Products Supporting Solely Case-Sensitive Regular Expressions Thesql_regcase()function converts each character in a string into a bracketed expression containing two characters. If the character is alphabetical, the bracket will contain both forms; otherwise, the original character will be left unchanged. Its prototype follows: string sql_regcase(string string) You might use this function as a workaround when using PHP applications to talk to other applications that support only case-sensitive regular expressions. Here’s how you would usesql_regcase()to convert a string: <?php
blog comments powered by Disqus |
|
|
|
|
|
|
|