<<

. 28
( 132 .)



>>

possible that something we didn™t cover will suit your needs perfectly. Please look
over the PHP manual for a complete list.

NL2BR() This function adds an HTML break (<br>) after each newline (\n) in a
string.

string nl2br (string string)
142 Part II: Working with PHP

Note that the newline characters will remain after going through this function.
For example, this code

$str = “jay
john
bob
stan”;

echo nl2br($str);

will print the following (note that this is the HTML source of the resulting page):

jay
<br>
john
<br>
bob
<br>
stan

MD5() md5() is a one-way algorithm that encrypts information.

string md5 (string str)

This function is often used for passwords. If you were to put a password in a text
file, it is possible that someone who had (legitimate) access to your system could
view the passwords. However, if you pass it through md5(), the correct password is
unknowable. For example, md5(“jay”) is baba327d241746ee0829e7e88117d4d5.
If this is what is entered in the text file, those who have rights to view the database
will not know what the clear text password is.


A safe password will be a lot more complex than jay. A cracker can (and
will) run an entire dictionary through md5() to see if something allows
entry to the system.



Regular expression functions
Regular expressions offer a method for complex pattern matching. If you™re new to
the concept of regular expressions, consider this: Given the string handling func-
tions you have seen so far, how can you insert a newline and a break (\n<br>) after
every 45 characters? Or, how can you find out if a string contains at least one
uppercase letter? You may be able to pull it off, but it won™t be pretty.
Chapter 6: PHP™s Built-in Functions 143

The following code will solve the problems posed by the previous two questions.

//insert \n<br> after each 45 characters
$new_str = ereg_replace(“(.{45})”, “\\1\n<br>”, $str);

//check if string contains uppercase letter
if (ereg(“[A-Z]”, $str))
{
echo “yes it does.”;
}

Statements like these may seem a bit opaque at first, but after working with
them for a while, you will grow to love the convenience they offer.


See Appendix G for a rundown on how regular expressions work.




Note that regular expressions are a good deal slower than string handling func-
tions. So if you have, for example, a simple replace that doesn™t require regular
expressions, use str_replace() and not ereg_replace().

REGULAR EXPRESSION FUNCTIONS USED IN THIS BOOK
The following regular-expression functions are used in the applications in this book.

EREG() ereg() tests whether a string contains a match for a regular expression.

int ereg (string pattern, string string [, array regs])

You can use this function in two ways. First, you can place a regular expression in
the first argument and search for its existence in the second argument. The function
will return TRUE or FALSE, depending on the outcome of the search. For example:

if ( ereg(“^http://.*”, $str) )
{
echo “This is a URL”;
}

Alternatively, the optional third argument is an array that is created from the
regular expression. The portions of the regular expression that will become ele-
ments in the array are indicated by parentheses in the regular expression.

ereg(“(....)-(..)-(..)”, $publish_date, $date_array);
144 Part II: Working with PHP

This example, which was taken from the content-management application in
Chapter 11, creates an array named $date_array, wherein the first element will be
the complete string matched by the regular expression. The next three elements in
the array will be the portions indicated by the parentheses. So $date_array[1]
will contain four characters, and $date_array[2] and date_array[3] will contain
two characters each.
So, after running this code

$publish_date = “2000-10-02”;
ereg(“(....)-(..)-(..)”, $publish_date, $date_array);

$date_array will contain the following:

[0] => 2000-10-02
[1] => 2000
[2] => 10
[3] => 02

Note that ereg() performs a case-sensitive match.

EREGI() 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, string
string)

For example, if you want to delete the querystring from a URL, you can use this:

$url= “http://www.mysqlphpapps.com/index.php?var=hello”;
$parsed_url = ereg_replace(“\?.*\$”, “”,$url);
echo $parsed_url;

The preceding code prints http://www.mysqlphpapps.com/index.php. This
regular expression matches a question mark and all characters that occur after it
until the end of the line. The question mark must be escaped with a backslash
because it has a specific meaning to the regular expression (regular expressions are
covered at http://www.php.net/manual/en/ref.pcre.php). Following the ques-
tion mark the regular expression matches any number of characters until the dollar
sign, which is the endline character. It needs to be escaped with a backslash because
otherwise PHP will think it represents a variable.
Chapter 6: PHP™s Built-in Functions 145

Often you will need a bit more functionality than this. What if you want to pre-
serve the string you are searching for in the replacement string? Or what if your
search contains distinct portions offset by sets of parentheses? Here™s a simple
example. We want to replace the current querystring by placing an additional
name=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, as follows:

$url= “http://www.mysqlphpapps.com/index.php?var=hello&var2=yup”;
$parsed_url = ereg_replace(“(\?.*&)”, “\\1newvar=here&”,$url);
echo $parsed_url;

This creates the following string:

http://www.mysqlphpapps.com/index.php?var=hello&newvar=here&var2=yup

Here 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 one
portion is indicated by additional parentheses, you can echo the others back into
the result by noting which portion you need.

$url= “this is a test “;
$parsed_url = ereg_replace(“(this.*a).*(test)”, “\\1 regular
expression \\2”,$url);
echo $parsed_url;

The result of these commands is the phrase this is a regular expression
test.
The regular expression matches everything between this and test. You can use
parentheses to indicate a substring that starts with this and ends with the letter a.
The next .* portion matches any number of characters. Finally, test is another
substring. These substrings are echoed back in the second argument, with \\1
echoing the first substring and \\2 echoing the second substring.
The regular expression match is case-sensitive.

EREGI_REPLACE() This function is the same as ereg_replace(), except that the
match is case-insensitive.

REGULAR EXPRESSION FUNCTION NOT USED
IN THIS BOOK ” sql_regcase()
This regular expression function, while not used in the examples in this book, is
still useful to know.
sql_regcase() alters strings so that you can use them in case-insensitive regu-
lar expressions.

string sql_regcase (string string)
146 Part II: Working with PHP

This might be of use if you are doing a regular-expression search in a database
server that doesn™t support case-insensitive regular expressions. It will save you
from having to type in every character in a string as both an uppercase and a low-
ercase 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 unlike
any others. If you have some experience with Perl, it™s likely that you™ve come to
love the additional power these regular expressions give you. If you don™t come
from a Perl background, you might enjoy learning a bit about the features.
PCREs are, however, a fairly large topic, one that Appendix G explains only
briefly. However, if you™re looking to get a good jump on learning about Perl™s reg-
ular expressions and how they can work for you, the information at http://
www.perldoc.com/perl5.8.0/pod/perlre.html is a good read. You™ll also find a
decent description of Perl regular expressions in the PHP manual, at http://
www.php.net/manual/en/ref.pcre.php.
The major reason for using PCRE functions is that they give you a choice
between “greedy” and “non-greedy” matching. For a quick example, take the fol-
lowing string:

$str = “I want to match to here. But end up matching to here”

Using ereg() or ereg_replace() you have no way to match from I to the first
occurrence of here. The following will not work as you might expect:

$str = “I want to match to here. But end up matching to here”;
$new_str = ereg_replace(“I.*here”, “Where”, $str);
echo $new_str;

This will print Where and nothing else. The entire string will be replaced. Using
ereg_replace() you cannot indicate that you want to match to the first occur-
rence of here. However, using preg_replace(), you can do the following:

$str = “I want to match to here. But end up matching to here”;
$new_str = preg_replace(“/I.*?here/”, “Where”, $str);
echo $new_str;

In this instance, .*? means “match all characters until the first occurrence.”
Chapter 6: PHP™s Built-in Functions 147

PCRE FUNCTIONS USED IN THIS BOOK
The following PCRE functions are used in the applications created in this book.

PREG_MATCH() This function is similar to the ereg() function in that you can
assign the optional third argument an array of matched subpatterns, if any are
found in the regular expression. preg_match returns the number of pattern
matches found, or FALSE if no match is found.

int preg_match (string pattern, string subject [, array matches])

PREG_REPLACE() This function makes replacements based on Perl regular
expressions.

mixed preg_replace (mixed pattern, mixed replacement, mixed subject
[, int limit])

preg_replace() is similar to ereg_replace(), though the pattern here must be
a Perl regular expression. It can also make use of \\digit to echo the matched sub-
string into the result. The optional fourth argument limits the number of replaces
that preg_replace makes.
Consider the following example:

preg_replace(“/(<br>|&nbsp;|[\s])*$/i”,””,$body);

Note that \s denotes all whitespace characters. This example will remove all occur-
rences of breaks (<br>), non-breaking spaces ($nbsp;), or white space (spaces, tabs,
new lines) at the end of the string in $body. This replacement is not case-sensitive
(the i flag determines that) to ensure that both <BR> and <br> are matched.
The parentheses indicate that you are specifying a pattern made up of several

<<

. 28
( 132 .)



>>