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

PHP and Regular Expressions

and splitting are to take place. In the absence of a third argument, matching and splitting proceed through the whole string.

The sql_regcase() Function

The sql_regcase() function is different from all the other functions in the ereg() family because it is used to create a regular expression pattern, rather than use one, as all the other functions do.

Try It Out

Using the sql_regcase() Function

1.Type the following code in a text editor:

<html>

<head>

<title>sql_regcase() Demo</title> </head>

<body>

<?php

$sequenceToMatch = “Doctor”;

$myPattern = sql_regcase($sequenceToMatch);

echo “<p>To match ‘$sequenceToMatch’ the sql_regcase() function produces: ‘$myPattern’.</p>”;

?>

</body>

</html>

2.Save the code as C:\inetpub\wwwroot\PHP\sql_regcaseDemo.php.

3.In Internet Explorer, enter the URL http://localhost/PHP/sql_regcaseDemo.php, and inspect the displayed results, as shown in Figure 23-12. The result produced is a sequence of character classes that allow case-insensitive matching.

Figure 23-12

How It Works

For each character in $sequenceToMatch a character class containing both an uppercase and lowercase character is created. So, for example, for the D of doctor, a character class [Dd] is created.

567

Chapter 23

Perl Compatible Regular Expressions

Perl Compatible Regular Expressions (PCRE) provides more modern and more powerful regular expression support in PHP than do ereg() and associated functions.

The following table summarizes the family of functions supported in PCRE in PHP. Each of these functions is covered in more detail a little later in the chapter.

Function

Description

 

 

preg_match()

Attempts to match a specified pattern in a specified test string

preg_match_all()

Attempts to match a specified pattern as many times as possi-

 

ble in a specified test string

preg_grep()

This function finds matches for a regular expression pattern in

 

an array

preg_quote()

This function takes a regular expression pattern and escapes

 

each character in it using a backslash

preg_replace()

Attempts to match a regular expression pattern in a string and

 

replaces any matches using a specified replacement string

preg_replace_callback()

Similar to preg_replace(), except that a callback is used to

 

define the replacement string

preg_split()

Splits a test string into an array of substrings, using a specified

 

pattern to define where splitting should take place

 

 

Pattern Delimiters in PCRE

PCRE includes support for developer-specified delimiters of regular expression patterns. The default delimiters are paired forward-slash characters. To supply a regular expression, you enclose it in paired delimiters inside the paired double quotes.

You can use any paired nonalphanumeric character as a delimiter, or you can use matched characters such as {}, <>, or ().

Try It Out

Various Delimiters

1.Type the following code into a text editor:

<html>

<head>

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

<body>

<?php

if (preg_match(“/Hel/”, “Hello world!”)) echo “<p>A match was found using paired ‘/’ as delimiter.</p>”;

if (preg_match(“.Hel.”, “Hello world!”)) echo “<p>A match was found using paired ‘.’ as delimiter.</p>”;

568

PHP and Regular Expressions

if (preg_match(“{Hel}”, “Hello world!”)) echo “<p>A match was found using matched ‘{‘ and ‘}’ as delimiters.</p>”;

if (preg_match(“(Hel)”, “Hello world!”)) echo “<p>A match was found using matched ‘(‘ and ‘)’ as delimiters.</p>”;

if (preg_match(“<Hel>”, “Hello world!”)) echo “<p>A match was found using matched ‘<’ and ‘>’ as delimiters.</p>”;

?>

</body>

</html>

2.Save the code as C:\inetpub\wwwroot\PHP\DelimiterTest.php.

3.Type the URL http://localhost/PHP/DelimiterTest.php into Internet Explorer, and inspect the displayed results, as shown in Figure 23-13.

Figure 23-13

How It Works

There are five examples of using the preg_match() function in this example. Each does basically the same thing, testing whether there is a match for the literal pattern Hel in the test string Hello world!, and if there is (there is in each of the examples), a message stating that there is a match and what delimiter was used is displayed.

The first example uses paired forward slashes as delimiters. I prefer using these because they are the default delimiters in Perl too:

if (preg_match(“/Hel/”, “Hello world!”)) echo “<p>A match was found using paired

‘/’ as delimiter.</p>”;

However, you can also use other paired nonalphanumeric delimiters. The second example uses paired period characters as the pattern delimiters:

if (preg_match(“.Hel.”, “Hello world!”)) echo “<p>A match was found using paired

‘.’ as delimiter.</p>”;

569

Chapter 23

The other three examples use matched pairs of characters. They are matched in the sense that a left curly brace, {, is matched with a right curly brace, }. The other matched pairs are ordinary parentheses, ( and ), and left and right angled brackets, < and >.

if (preg_match(“{Hel}”, “Hello world!”)) echo “<p>A match was found using matched ‘{‘ and ‘}’ as delimiters.</p>”;

if (preg_match(“(Hel)”, “Hello world!”)) echo “<p>A match was found using matched

‘(‘ and ‘)’ as delimiters.</p>”;

if (preg_match(“<Hel>”, “Hello world!”)) echo “<p>A match was found using matched ‘<’ and ‘>’ as delimiters.</p>”;

I suggest that unless there is good reason to vary from using the forward slash, you use that. Most developers will be familiar with the use of paired forward slashes as regular expression delimiters, so using them is least likely to cause confusion for others reading your code. If you use another delimiter, I suggest that you add a comment to document your choice for any developer who has to maintain your code.

Escaping Pattern Delimiters

When a character such as the forward slash is used as a regular expression pattern delimiter in PHP, it is necessary to escape that delimiter character if you desire to match that character literally. For example, if the forward slash is used as a pattern delimiter, and you want to match an HTTP URL that references the com, net, org, info, and biz top-level domains, you must escape each forward slash in the pattern, which is the first argument of the preg_match() function. For example:

preg_match(‘/http:\/\/.*\.(com|net|org|info|biz)\/.*/’, $testString)

Matching Modifiers in PCRE

It will come as little surprise that the matching modifiers in Perl Compatible Regular Expressions are based on those in Perl. The following table summarizes the matching modifiers available in the PCRE functionality in PHP.

Matching Modifier

Description

 

 

i

Causes matching to be case insensitive.

m

Multiline. Alters the effect of the ^ and $ metacharacters. With multi-

 

line chosen, ^ matches the position at the beginning of a line, and $

 

matches the position at the end of a line.

s

Modifies what the period metacharacter matches. With the s modi-

 

fier selected, the period metacharacter matches all characters. With-

 

out s being selected, the period metacharacter matches all characters

 

except newline.

x

Modifies how whitespace inside a pattern is processed. With x

 

selected unescaped, whitespace characters are ignored.

A

Constrains matching to the beginning of the test string.

D

If the D modifier is set, the $ metacharacter matches only at the end

 

of the test string. If the m matching modifier is set, the D modifier is

 

ignored.

 

 

570