Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Regular Expressions 2005.pdf
Скачиваний:
95
Добавлен:
17.08.2013
Размер:
25.42 Mб
Скачать

PHP and Regular Expressions

Figure 23-4

How PHP Structures Suppor t for Regular Expressions

Regular expression support in PHP is provided by two different sets of functions. One, ereg() and related functions, has been available in PHP for some time (you can use it with PHP3 Web servers if you wish). The other, Perl Compatible Regular Expressions (PCRE), is more recent and has more regular expression functionality. Most of the descriptions and examples in this chapter will focus on the PCRE functionality.

If you need compatibility with older versions of PHP, the ereg() set of functions may be your only choice. On the other hand, if you need the functionality that is absent from the ereg() family, PCRE may be the most viable option, and if necessary, you may need to upgrade the PHP version on your Web server(s).

The ereg() Set of Functions

The ereg() set of functions is based on POSIX regular expressions. The following table summarizes the functions that relate to the ereg() function.

Function

Description

 

 

ereg()

Attempts to match a regular expression pattern against a string

 

case sensitively.

eregi()

Attempts to match a regular expression pattern against a string

 

case insensitively.

ereg_replace()

Attempts to match a regular expression pattern case sensi-

 

tively, and if matches are found, they are replaced.

 

Table continued on following page

553

Chapter 23

Function

Description

 

 

eregi_replace()

Attempts to match a regular expression pattern case insensi-

 

tively, and if matches are found, they are replaced.

split()

Splits a string into an array, based on matching of a regular

 

expression, case-sensitive matching.

spliti()

Splits a string into an array, based on matching of a regular

 

expression, case-insensitive matching.

sql_regcase()

Creates a valid regular expression pattern to attempt case-

 

insensitive matching of a specified string.

 

 

The ereg() Function

The ereg() function matches case sensitively. The ereg() function can be used with two arguments or three. When ereg() is used with two arguments, the first argument is a string value, which is a regular expression pattern. The second argument is a test string.

For example, if you wanted to find whether there is a match for the literal pattern the in the test string

The theatre is a favorite of thespians., you could use the following code:

ereg(‘the’, “The theatre is a favorite of thespians”);

Because ereg() matches case sensitively, there are two matches: the first three characters of theatre and of thespians.

Before looking at how to use ereg() with three arguments, work through the following example, which uses the ereg() function with two arguments. It shows a very simple use of the ereg() function to match a literal regular expression pattern, Hel, against a literal string value, Hello world!.

Try It Out

A Simple ereg() Example

1.In a text editor, enter the following code. You can use Notepad if you have no other text editor available.

<html>

<head>

<title>Simple ereg() Regex Test</title> </head>

<body>

<?php

if (ereg(“Hel”, “Hello world!”)) echo “<p>A match was found.</p>” ?>

</body>

</html>

2.Save the file as C:\inetpub\PHP\SimpleRegexTest.php. Modify the location if your Web server is not located on the local machine or you created the PHP directory in a different location.

554

PHP and Regular Expressions

3.In your preferred Web browser, enter the URL http://localhost/PHP/SimpleRegexTest.php; press the Return key; and inspect the Web page that is displayed, as shown in Figure 23-5. The message A match was found. is displayed.

Figure 23-5

How It Works

The file SimpleRegexTest.php includes HTML/XHTML markup and PHP code. The PHP code is very simple:

<?php

if (ereg(“Hel”, “Hello world!”)) echo “<p>A match was found.</p>” ?>

The <?php marks where the PHP code begins, and the ?> marks where the PHP code ends. The middle line is the PHP code itself.

The if statement tests the value returned by the ereg() function. The ereg() function takes two arguments in this example. The first argument is the string Hel, which is interpreted as a literal regular expression pattern. The second argument is the string Hello world!, which is the test string. In other words, the PHP processor attempts to find the pattern Hel in the string Hello world!. Not surprisingly, a match is found, so the ereg() function returns the value 1, indicating the presence of a match. The value 1 is interpreted as equivalent to True, so the code controlled by the if statement is executed:

echo “<p>A match was found.</p>”

Typically, a PHP statement has to end with a semicolon. Because this very simple example has only one line of PHP code, it isn’t necessary to add a semicolon. In other examples in this chapter, your code almost certainly won’t run correctly if you omit the semicolon at the end of PHP statements.

The echo statement causes a string to be inserted into the Web page at the position where the PHP code existed in the Web page SimpleRegexTest.php. That string, which includes HTML/XHTML markup, is inserted on the server between the start tag, <body>, and the end tag, </body>. The source code for the Web page, as delivered to the client, is shown in Figure 23-6.

555

Chapter 23

Figure 23-6

This example simply used the boolean value returned by the ereg() function to control the literal text that was added to the Web page. The next example looks at how the content of individual matches can be manipulated when using the ereg() function.

The ereg() Function with Three Arguments

When the ereg() function is used with three arguments, the first argument is the regular expression pattern expressed as a string value, the second argument is the test string, and the third argument specifies an array where the matches are to be stored.

Try It Out

The ereg() Function with Three Arguments

1.In your favorite text editor, enter the following code:

<html>

<head>

<title>Splitting a date using ereg()</title> </head>

<body>

<?php

$myPattern = ‘([12][0-9]{3}) ([01][0-9]) ([0123][0-9])’; $testString = gmdate(“Y m d”);

echo “<p>The date is now: $testString</p>”; $myResult = ereg($myPattern, $testString, $matches); if ($myResult)

{

echo “<p>A match was found when testing case sensitively.</p>”; echo “<p>Expressed in MM/DD/YYYY format the date is now, $matches[2]/$matches[3]/$matches[1].</p>”;

}

else

{

echo “<p>No match was found when testing case sensitively.</p>”;

}

?>

</body>

</html>

2.Save the code as SplitDate.php in the PHP directory in the wwwroot directory of your IIS installation. In other words, save the code as C:\inetpub\wwwroot\PHP\SplitDate.php.

556

PHP and Regular Expressions

3.Enter the URL http://localhost/PHP/SplitDate.php in Internet Explorer, and inspect the displayed results, as shown in Figure 23-7. The code was run on September 22, 2004. Notice that on the final line in the Web browser, that date is displayed as 09/22/2004, having originally been 2004 09 22 when formatted by the gmdate() function.

Figure 23-7

How It Works

First, look at the pattern that is used:

([12][0-9]{3}) ([01][0-9]) ([0123][0-9])

Because the date is created by the PHP gmdate() function, you don’t, realistically, need to worry much about the function returning invalid dates. So you can use a fairly crude pattern here.

This pattern first matches a single numeric digit: a 1 or 2. This makes sense if the dates you are dealing with are in the twentieth and twenty-first centuries. Then three further numeric digits are matched. This part of the pattern is contained in paired parentheses, so it is captured. You will see a little later how the captured group is handled and retrieved. This pattern component matches a four-digit year from the twentieth or twenty-first century.

A single space character follows. Then comes a further captured group beginning with a 0 or 1, which is followed by a numeric digit. This matches the numeric value of the month. Theoretically, it will match “months” such as the 19th month of a year. However, because the gmdate() function is very unlikely ever to return a spurious date like that, it’s an issue you don’t need to worry about in this example.

Then follows a single space character and a further captured group beginning with a 0, 1, 2, or 3 followed by a numeric digit. This matches the numeric value of the day of the month. Theoretically, it will match “days” such as the 39th of January. Again, because the gmdate() function is very unlikely ever to return a spurious date, you don’t need to worry about it.

First, you assign the pattern, as a string, to the $myPattern variable:

$myPattern = ‘([12][0-9]{3}) ([01][0-9]) ([0123][0-9])’;

Then you use the gmdate() function to assign a string value to the $testString variable. The arguments to the gmdate() function specify the formatting of the date returned. In this case, Y specifies that the year is returned as a four-digit form, 2004; the m specifies that the month is expressed as a number between 1 and 12; and the d specifies that the day of the month is expressed as a number from 1 to 31:

557

Chapter 23

$testString = gmdate(“Y m d”);

Then you use the echo statement to display the format in which the current date was returned. If you are used to languages such as JavaScript, you may find it surprising that the $testString variable is just written inside the paired double quotes rather than having literal text inside paired double quotes and using the + concatenation operator. In PHP, you simply write a variable inside the paired quotes, and the PHP processor knows it is intended that the variable’s value be displayed:

echo “<p>The date is now: $testString</p>”;

Then you use the ereg() function with three arguments to assign a value to the $myResult variable. If one or more matches exist, the $myResult variable contains the boolean value True (1), and if there is no match, it contains the boolean value False (0).

The first argument is a pattern, and the second argument is the test string, as before. The third argument is an array containing the components of the matched text. The elements of that array are determined by the capturing parentheses, which were mentioned when you looked at the components of the regular expression pattern:

$myResult = ereg($myPattern, $testString, $matches);

You test to see if there was a match:

if ($myResult)

If there was, you use two echo statements to add content to the Web page:

{

You simply state that a match was found:

echo “<p>A match was found when testing case sensitively.</p>”;

Then you indicate that the date will reorder the date. This is done using

be returned in MM/DD/YYYY format, which means that you must $matches[2]/$matches[3]/$matches[1].

The value of the whole match is in array element 0. The pattern that matched the date contained three pairs of capturing parentheses. The result of the first pair goes in $matches[1], the result of the second pair in $matches[2], and so on. So the year is in $matches[1], the month in $matches[2], and the day in $matches[3].

Because you want to return MM/DD/YYYY, you use the sequence $matches[2]/$matches[3]/ $matches[1], which means return the value in $matches[2] (the month) followed by a literal forwardslash character, followed by the value in $matches[3] (the day), followed by a literal forward-slash character, followed by the value in $matches [1] (the year).

So the value that was originally displayed as 2004 09 20 is transformed into 09/20/2004:

echo “<p>Expressed in MM/DD/YYYY format the date is now,

$matches[2]/$matches[3]/$matches[1].</p>”;

558