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

Chapter 23

How It Works

The original string Star Training Company is assigned to the $myString variable:

$myString = “Star Training Company.”;

The string value returned by the preg_replace() function is assigned to the $newString variable. The pattern used in the preg_replace() function is a literal pattern Star. The replacement text is Moon. Replacement is to take place in the string specified by the $myString variable. Essentially, any occurrence of Star is replaced by Moon:

$newString = preg_replace(“/Star/”, “Moon”, $myString);

The original and replaced strings are displayed to the user:

echo “<p>The original string was: ‘$myString’.</p>”;

echo “<p>After replacement the string is: ‘$newString’.</p.>”;

Using the preg_replace_callback() Function

The preg_replace_callback() function behaves essentially identically to the preg_replace() function, except that the second argument to the preg_replace_callback() function is a callback to another function. The callback can be to any user-specified function.

Using the preg_split() Function

The preg_split() function splits a test string according to a regular expression pattern. It returns an array. It takes two mandatory arguments and two optional arguments. The two mandatory arguments are a regular expression pattern and a test string. The third (optional) argument is an int value that specifies the maximum number of splits. The fourth (optional) argument is an int value that indicates the flags that are set.

Try It Out

The preg_split() Function

1.Type the following code in a text editor:

<html>

<head>

<title>A preg_split() Example</title> </head>

<body>

<?php

$myCSV = “Oranges, Apples, Bananas, Kiwi Fruit, Mangos”; $myArray = preg_split(“/,/”, $myCSV);

echo “<p>The original string was: ‘$myCSV’.</p>”;

echo “<p>After splitting the array contains the following values:</p.><br />”; print_r(array_values($myArray));

?>

</body>

</html>

580

PHP and Regular Expressions

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

3.Type the following URL in Internet Explorer: http://localhost/PHP/preg_split.php. Inspect the displayed results, as shown in Figure 23-20.

Figure 23-20

How It Works

The example splits a comma-separated list of values that are contained in a single string into an array. First, the test string is assigned to the $myCSV variable:

$myCSV = “Oranges, Apples, Bananas, Kiwi Fruit, Mangos”;

Then the array returned by the preg_split() function is assigned to the $myArray variable. The first argument to preg_split() is a regular expression pattern containing a comma, so the test string $myCSV is split at each comma. Splitting takes place at each occurrence of the comma because no third argument to the preg_split() function is specified:

$myArray = preg_split(“/,/”, $myCSV);

Two echo statements are used to display the original string and information to the user. The print_r() function is used with the array_values() function to display the values in the $myArray variable:

echo “<p>The original string was: ‘$myCSV’.</p>”;

echo “<p>After splitting the array contains the following values:</p.><br />”; print_r(array_values($myArray));

The Metacharacters Suppor ted in PHP

This description of the supported metacharacters will treat the ereg() and preg() sets of functions separately. So if you wish to use one of these sets of functions only, you can easily see which functionality is supported in your chosen set of functions.

581

Chapter 23

Supported Metacharacters with ereg()

The following table summarizes the metacharacters supported in PHP when you are using the ereg() family of functions.

Metacharacter

Description

 

 

\d

Not supported. Use the character class [0-9] instead.

\D

Not supported. Use the negated character class [^0-9]

 

instead.

\w

Not supported. Use the character class [A-Za-z0-9_] or

 

the POSIX character class [:alnum:] instead with ereg().

\W

Not supported. Use the negated character class

 

[^A-Za-z0-9_] instead with ereg().

?

Quantifier. Matches zero or one occurrence of the preceding

 

character or group.

*

Quantifier. Matches zero or more occurrences of the preced-

 

ing character or group.

+

Quantifier. Matches one or more occurrences of the preced-

 

ing character or group.

{n,m}

Quantifier. Matches at least n occurrences and no more than

 

m occurrences of the preceding character or group.

. (period character)

Matches any character except newline.

^

Matches the position at the beginning of a string.

$

Matches the position at the end of a string.

[...]

Character class. Matches a single occurrence of any charac-

 

ter contained between the square brackets.

[^...]

Negated character class. Matches a single occurrence of any

 

character not contained between the square brackets.

 

 

Using POSIX Character Classes with PHP

The ereg() functionality is based on POSIX. Therefore, you can use POSIX character classes such as [:alnum:] with ereg() and associated functions.

The following table lists the most commonly used POSIX character classes.

582

 

 

 

 

PHP and Regular Expressions

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Character Class

Description

 

 

 

 

 

 

 

 

[:alnum:]

 

Matches alphabetic or numeric characters

 

 

[:alpha:]

 

Matches alphabetic characters

 

 

[:space:]

 

Matches whitespace characters

 

 

[:blank:]

 

Matches a space character or a tab character

 

 

[:digit:]

 

Matches a numeric digit

 

 

[:lower:]

 

Matches a lowercase alphabetic character

 

 

[:upper:]

 

Matches an uppercase alphabetic character

 

 

 

 

Try It Out

 

Using [:alnum:] with ereg()

 

1.Enter the following code in your favorite text editor:

<html>

<head>

<title>ereg() [:alnum:] Test</title> </head>

<body>

<?php

$match = ereg(‘[[:alnum:]]+’, “Hello world!”, $matches); if ($match)

{

echo “<p>A match was found.</p>”; echo “<p>$matches[0]</p>”;

}

?>

</body>

</html>

2.Save the file as EregAlnumTest.php.

3.In Internet Explorer, enter the URL http://localhost/PHP/EregAlnumTest.php, and inspect the displayed result, as shown in Figure 23-21.

Figure 23-21

583

Chapter 23

How It Works

The POSIX character class [:alnum:] matches alphabetic characters and numeric digits. Unlike the \w metacharacter, it does not match the underscore character.

The first argument to the ereg() function matches one or more alphanumeric characters. Notice that in PHP, the [:alnum:] character class is contained inside another pair of square brackets.

Matching is attempted against the test string, Hello world!. The ereg() function has three arguments, so the value of groups in the first match, if there is one, is returned in the $matches array:

$match = ereg(‘[[:alnum:]]+’, “Hello world!”, $matches);

The if statement tests if there is a match, using the value of the $match variable:

if ($match)

In this case, the user is informed that there is a match:

{

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

The matched text is contained in the zeroth group in the $matches array. That matched character sequence is displayed to the user:

echo “<p>$matches[0]</p>”;

}

The next example uses the POSIX [:space:] character class, which matches whitespace characters.

Try It Out

Using the [:space:] Character Class

1.Enter the following code in a text editor:

<html>

<head>

<title>ereg() [:space:] Test</title> </head>

<body>

<?php

$match = ereg(‘o[[:space:]]+w’, “Hello world!”, $matches); if ($match)

{

echo “<p>A match was found.</p>”; echo “<p>$matches[0]</p>”;

}

?>

</body>

</html>

584