Skip to main content

Posts

More on Perl-style regular expressions

• preg_match_all() -                                This function is similar to preg_match(); however, rather than searching for just one occurrence of the search parameter, this function will find all occurrences. In addition, each occurrence found will be placed into an array variable entered into the third input parameter. The array created will contain various sections of the subpatterns contained in the search pattern. Just like preg_match(), if there is a match, the function returns a TRUE, if the regular expression does not find a match, a FALSE is returned. <?php $text = "mysql_close, mysql_connect and mysql_error are all PHP functions used when working with MySQL."; if (preg_match_all("/mysql\w+/",$text,$mysql_commands) { print_r($mysql_commands); } else { print "No mysql commands were found"; } ?> • preg_quote() -                         This function places a backslash in front of all special regular expression charact

Using Perl-style regular expressions

Within PHP there are seven functions used for searching strings using Perl-style regular expressions: • preg_match() -                          This function utilizes the Perl-style regular expressions to search strings provided for a match. If there is a match, the function returns a TRUE, if the regular expression does not find a match, a FALSE is returned. <?php $phone_number = "1900Atlanta"; if (preg_match("/.\D/",$phone_number)) print "The Phone Number must contain only numeric digits"; ?> • preg_grep() -                        This function is similar to the preg_match() function, but works only on arrays. The preg_grep() function is also useful in that it creates a new array based on those matches found in the search array. <?php $product_codes = ("NZ3456", "SUS5678", "SNZ3294", "US4678"); $NZ_Codes = preg_grep("/NZ/", $product_codes); print_r($NZ_Codes); //NZ3456 and

Perl-style

The Perl programming language is exceptional at parsing strings by providing a comprehensive regular expression language to the programmer. Rather than creating their own regular expression language, the developers of PHP made the Perl regular expression syntax available to PHP users. The Perl-style regular expression syntax was built from the POSIX regular expression syntax and thus hold many of the same features. In fact, PHP programmers can use many of the same POSIX regular expression syntax when using Perl-style regular expression syntax. The basic Perl-style regular expression syntax involves using forward slashes (/ /) to identify the pattern that will be searched for: /mysql/ will find any string that contains the pattern "mysql", /m+/ will find any string that contains the letter "m" followed by one or more characters (mysql, mom, mudd, my, etc.) and /m{2,4}/ will find any string that contains the letter "m" followed by 2 or 4 character

POSIX regular expressions(SQL)

• sql_regcase -                         This function converts each alphabetical character into its equivalent lower and upper case representation surrounded by brackets. For those characters that are not alphabetical, no change is taken place. <?php $text = "MySQL's website is http://www.mysql.com"; print sql_regcase($text); // Outputs [Mm][Yy][Ss][Qq][Ll]'[Ss] [Ww][Ee][Bb][Ss][Ii][Tt] // [Ee] [Ii][Ss] [Hh][Tt][Tt][Pp]://[Ww][Ww][Ww].[Mm][Yy][Ss] // [Qq][Ll].[Cc][Oo][Mm] ?>

POSIX regular expressions(SPLIT)

split -           This function divides strings based on the pattern that it searches for and stored the separated text into an array. The searches are case-sensitive when dealing with alphabetical characters. <?php $csv_text = "5, \"Amsterdam\", \"NLD\", \"Noord-Holland\", 731200"; $csv_array = split("[,]",$csv_text); foreach ($csv_array as $csv_column) { print "$csv_column<br>"; } ?> The script above would create an array called $csv_row that contains the following element: [0] = 5, [1]="Amsterdam", [2]="NLD", [3]="Noord-Holland", [4]=731200). The foreach iterative control statement would print each of these elements on the screen on their own lines. A third component can be placed in the split function that would limit the number of array elements that would be created. By adding 3 to the split function in the above script (split("[,]",$csv_text, 3) the

More on POSIX regular expressions

• ereg_replace()-                          This function is equivalent to ereg() in its case-sensitive searching capabilities; however, it adds the ability to replace the characters it is searching for with new characters instead of just simply locating them. If the search pattern is not found, the string it searched against is unchanged. <?php $url = "MySQL http://www.mysql.com"; print ereg_replace("http://([a-zA-Z0-9./-]+)$", "<a href=\"\\0\">\\0</a>", $url); ?> The script above would change the text "MySQL http://www.mysql.com" to "MySQL <a href="http://www.mysql.com">http://www.mysql.com</a>". This change would allow the http reference to be replaced with the HTML code required to create a web link. The three components of this function include the pattern to search for ("http://([a-zA-Z0-9./-]+)$"), the replacement string ("<a href=\"\\0\&qu

Using POSIX regular expressions

Within PHP there are seven functions used for searching strings using POSIX-style regular expressions: • ereg () -               This function utilizes the POSIX-style regular expressions to search strings provided for a match and is case-sensitive. If there is a match, the function returns a TRUE, if the regular expression does not find a match, a FALSE is returned. <?php $phone_number = "1900Atlanta"; if (ereg("[^0-9]",$phone_number)) print "The Phone Number must contain only numeric digits"; ?> The result of the above php script, which tests for any characters in the $phone_number variable that are not numeric digits, would be the printing of "The Phone Number must contain only numeric digits". Another way to use the ereg() function is to utilize the optional parameter regs (the third parameter) to break up a string. The following demonstrates how this would work: <?php $phone_number = "515-435-6789"; $