Skip to main content

Posts

Comparing Strings

As stated, PHP has poweful regular expressions that provide programmers with the tools necessary to complete difficult searching tasks. In addition to this regular expression capability, PHP provides built-in functionality for performing a wide range of string-specific tasks. Many of these tasks are based on string comparison operations. String Length One of the simplest comparison operators of strings is determining if the lengths are equivalent. This involves determining the length of the strings and then acting upon those lengths. The length function built-in to PHP is strlen() and is one of the more widely used string expressions. <?php function chk_passwd($input) { if (strlen($input) < 7) { print "Password is too short"; } else if (strlen($input) > 10) { print "Password is too long"; }else { print "Password is the proper length, proceed<br>"; } } chk_passwd("doggy1"); chk_passwd("Diabolical");

Perl-style regular expressions(preg_split )

• preg_split -                     This Perl-style function is equivalent to the split() function previously discussed with the exception of allowing Perl-style regular expressions. The third optional component of the preg_split() function can be entered to limit the number of changes that take place. Failing to enter a numeric limit (or a -1), ensures that there is no limit on the changes that can take place. <?php $csv_text = "5, \"Amsterdam\", \"NLD\", \"Noord-Holland\", 731200"; $csv_array = preg_split("/\,/",$csv_text); foreach ($csv_array as $csv_column) { print "$csv_column<br>"; } ?> The script above creates 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 prints each of these elements on the screen on their own lines. A third component

Perl-style regular expressions( preg_replace_callback)

• preg_replace_callback() -                                            With preg_replace(), the function itself is responsible for handling the replacement procedure. With preg_replace_callback(), the handling of the replacement procedure is handed off to another function to take care of. The syntax is identical to preg_replace() except where the replacement text would be located, a function name is present. This function is responsible for handling any of the replacement changes to the text that matches the pattern searched. <?php $text = "Dear <pnm>S</pnm>Ortiz,<br>&nbsp;&nbsp;&nbsp;&nbsp;I would like to thank you and <pnf>F</pnf>Picard for taking the time in talking to me today. Please feel free to contact me if you have any additional questions.<br>Sincerely,<br><pnm>E</pnm>Bob Riker"; function proper_name_m($matches) { $titles = array( 'E' => 'Mr. ', 'S'

Perl-style regular expressions( preg_replace)

• preg_replace() -                              This Perl-syntax function is equivalent to the ereg_replace() function. This function works by replacing all occurrences of the pattern searched with the replacement characters and then returns the modified result. The fourth parameter is optional, but when a number is entered it determines how many occurrences of the pattern searched will be replaced. If no number is entered, all occurrences of the search pattern will be replaced. <?php $url = "MySQL http://www.mysql.com"; print preg_replace("/http:\/\/(.*)/","<a href=\"\${0}\">\${0}</a>", $url); ?> The script above changes 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:\/\/(.*)/"), the replacement string ("<a href=\"\${0}\">\${0}</a>") and then finally the

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