[ Pobierz całość w formacie PDF ]
.An isset() will always test TRUE.So, after running this code:$publish_date = 2000-10-02 ;ereg( (.)-(.)-(.) , $publish_date, $date_array);$date_array would contain the following:[0] => 2000-10-02[1] => 2000[2] => 10[3] => 02[4] =>[5] =>[6] =>[7] =>[8] =>[9] =>Note that ereg() performs a case-sensitive match.3537-4 ch06.f.qc 12/15/00 15:22 Page 137Chapter 6: PHP s Built-in Functions 137EREGI() This function is a case-insensitive version of ereg().int eregi (string pattern, string string [, array regs])EREG_REPLACE() You can use this function for string replacement based on com-plex string patterns.string ereg_replace (string pattern, string replacement, stringstring)For example, if you wanted to delete the querystring from a URL, you could use this:$url= http://www.phpmysqlbook.com/index.php?var=hello ;$parsed_url = ereg_replace( \?.*\$ , ,$url);echo $parsed_url;This would print http://www.phpmysqlbook.com/index.php.This regularexpression matches a question mark, and all characters that occur after it until theend of the line.The question mark must be escaped with a backslash because it hasa specific meaning to the regular expression.Following the question mark wematch any number of characters until the dollar sign, which is the endline charac-ter.It needs to be escaped with a backslash because without the backslash, PHP willthink the character represents a variable.But often you will need a bit more functionality than this.What if you want topreserve the string you are searching for in the replacement string? Or what if yoursearch contains distinct portions offset by sets of parentheses? Here s a simpleexample.We want to replace the current querystring by placing an additionalname=value pair between the two name=value pairs currently in the string.That is,we want to put newvar=here after var=hello and before var2=yup.$url= http://www.phpmysqlbook.com/index.php?var=hello&var2=yup ;$parsed_url = ereg_replace( (\?.*&) , \\1newvar=here& ,$url);echo $parsed_url;This creates the following string:http://www.phpmysqlbook.com/index.php?var=hello&newvar=here&var2=yupHere the single set of parentheses indicates portion 1.Then, by using the nota-tion \\1, we can include that portion in the newly created string.If more than oneportion is indicated by additional parentheses, you can echo the others back intothe result by noting which portion you need.$url= this is a test ;$parsed_url = ereg_replace( (this.*a).*(test) , \\1 regular3537-4 ch06.f.qc 12/15/00 15:22 Page 138138 Part II: Working with PHPexpression \\2 ,$url);echo $parsed_url;The result of these commands is: this is a regular expression test.The regular expression matches everything between this and test.We useparentheses to indicate a substring that starts with this and moves to the letter a.The next.* portion matches any number of characters.Finally, test is another sub-string.These substrings are echoed back in the second argument, with \\1 echoingthe first substring and \\2 echoing the second substring.The regular expression match is case-sensitive.EREGI_REPLACE() This is the same as ereg_replace(), except that the match iscase-insensitive.REGULAR EXPRESSION FUNCTIONS NOT USED IN THIS BOOKThe following regular expression functions, while not used in the examples in thisbook, are still useful to know.SQL_REGCASE() This nifty little function will alter strings so that you can usethem in case-insensitive regular expressions.string sql_regcase (string string)This might be of use if you are doing a regular expression search in a databaseserver that doesn t support case-insensitive regular expressions.It will save youfrom having to type in every character in a string as both an uppercase and lower-case letter.For example:echo sql_regcase( this string );produces:[Tt][Hh][Ii][Ss] [Ss][Tt][Rr][Ii][Nn][Gg]PERL-COMPATIBLE REGULAR EXPRESSIONS (PCRE)For years, the Perl programmers of the world have had regular expressions unlikeany others.If you have some experience with Perl, it s likely that you ve come tolove the additional power these regular expressions give you.If you don t comefrom a Perl background, you might enjoy learning a bit about the features.PCREs are, however, a fairly large topic, one that Appendix F explains only briefly.However, if you re looking to get a good jump on learning about Perl s regularexpressions and how they can work for you, the information at the following URL isa good read: http://www.perl.com/pub/doc/manual/html/pod/perlre.html.There is also a decent description of Perl regular expressions in the PHP4 manual:http://www.php.net/manual/pcre.pattern.syntax.html.3537-4 ch06.f.qc 12/15/00 15:22 Page 139Chapter 6: PHP s Built-in Functions 139The major reason for using PCRE functions is that they give you choice between greedy and non-greedy matching.For a quick example, take the following string.$str = I want to match to here.But end up matching to hereUsing ereg() or ereg_replace() there is no way to match from I to the firstoccurrence of here [ Pobierz całość w formacie PDF ]
zanotowane.pl doc.pisz.pl pdf.pisz.pl odbijak.htw.pl
.An isset() will always test TRUE.So, after running this code:$publish_date = 2000-10-02 ;ereg( (.)-(.)-(.) , $publish_date, $date_array);$date_array would contain the following:[0] => 2000-10-02[1] => 2000[2] => 10[3] => 02[4] =>[5] =>[6] =>[7] =>[8] =>[9] =>Note that ereg() performs a case-sensitive match.3537-4 ch06.f.qc 12/15/00 15:22 Page 137Chapter 6: PHP s Built-in Functions 137EREGI() This function is a case-insensitive version of ereg().int eregi (string pattern, string string [, array regs])EREG_REPLACE() You can use this function for string replacement based on com-plex string patterns.string ereg_replace (string pattern, string replacement, stringstring)For example, if you wanted to delete the querystring from a URL, you could use this:$url= http://www.phpmysqlbook.com/index.php?var=hello ;$parsed_url = ereg_replace( \?.*\$ , ,$url);echo $parsed_url;This would print http://www.phpmysqlbook.com/index.php.This regularexpression matches a question mark, and all characters that occur after it until theend of the line.The question mark must be escaped with a backslash because it hasa specific meaning to the regular expression.Following the question mark wematch any number of characters until the dollar sign, which is the endline charac-ter.It needs to be escaped with a backslash because without the backslash, PHP willthink the character represents a variable.But often you will need a bit more functionality than this.What if you want topreserve the string you are searching for in the replacement string? Or what if yoursearch contains distinct portions offset by sets of parentheses? Here s a simpleexample.We want to replace the current querystring by placing an additionalname=value pair between the two name=value pairs currently in the string.That is,we want to put newvar=here after var=hello and before var2=yup.$url= http://www.phpmysqlbook.com/index.php?var=hello&var2=yup ;$parsed_url = ereg_replace( (\?.*&) , \\1newvar=here& ,$url);echo $parsed_url;This creates the following string:http://www.phpmysqlbook.com/index.php?var=hello&newvar=here&var2=yupHere the single set of parentheses indicates portion 1.Then, by using the nota-tion \\1, we can include that portion in the newly created string.If more than oneportion is indicated by additional parentheses, you can echo the others back intothe result by noting which portion you need.$url= this is a test ;$parsed_url = ereg_replace( (this.*a).*(test) , \\1 regular3537-4 ch06.f.qc 12/15/00 15:22 Page 138138 Part II: Working with PHPexpression \\2 ,$url);echo $parsed_url;The result of these commands is: this is a regular expression test.The regular expression matches everything between this and test.We useparentheses to indicate a substring that starts with this and moves to the letter a.The next.* portion matches any number of characters.Finally, test is another sub-string.These substrings are echoed back in the second argument, with \\1 echoingthe first substring and \\2 echoing the second substring.The regular expression match is case-sensitive.EREGI_REPLACE() This is the same as ereg_replace(), except that the match iscase-insensitive.REGULAR EXPRESSION FUNCTIONS NOT USED IN THIS BOOKThe following regular expression functions, while not used in the examples in thisbook, are still useful to know.SQL_REGCASE() This nifty little function will alter strings so that you can usethem in case-insensitive regular expressions.string sql_regcase (string string)This might be of use if you are doing a regular expression search in a databaseserver that doesn t support case-insensitive regular expressions.It will save youfrom having to type in every character in a string as both an uppercase and lower-case letter.For example:echo sql_regcase( this string );produces:[Tt][Hh][Ii][Ss] [Ss][Tt][Rr][Ii][Nn][Gg]PERL-COMPATIBLE REGULAR EXPRESSIONS (PCRE)For years, the Perl programmers of the world have had regular expressions unlikeany others.If you have some experience with Perl, it s likely that you ve come tolove the additional power these regular expressions give you.If you don t comefrom a Perl background, you might enjoy learning a bit about the features.PCREs are, however, a fairly large topic, one that Appendix F explains only briefly.However, if you re looking to get a good jump on learning about Perl s regularexpressions and how they can work for you, the information at the following URL isa good read: http://www.perl.com/pub/doc/manual/html/pod/perlre.html.There is also a decent description of Perl regular expressions in the PHP4 manual:http://www.php.net/manual/pcre.pattern.syntax.html.3537-4 ch06.f.qc 12/15/00 15:22 Page 139Chapter 6: PHP s Built-in Functions 139The major reason for using PCRE functions is that they give you choice between greedy and non-greedy matching.For a quick example, take the following string.$str = I want to match to here.But end up matching to hereUsing ereg() or ereg_replace() there is no way to match from I to the firstoccurrence of here [ Pobierz całość w formacie PDF ]