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

PHP and Regular Expressions

If no match were present, you would simply display a message indicating that

}

else

{

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

}

So groups that are captured using paired parentheses are, if you specify a variable as the third argument to the ereg() function, each stored in the elements of the array specified by the third argument.

The eregi() Function

The eregi() function does almost the same as the ereg() function, except that it matches case insensitively.

The following example shows the results when using ereg() and eregi() on the same test text.

Try It Out

The eregi() Function

1.In a text editor, enter the following code:

<html>

<head>

<title>ereg() and eregi() Test to match [A-Z][0-9]</title> </head>

<body>

<?php

$myPattern = ‘[A-Z][0-9]’; $testString = “a9”;

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

{

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

}

else

{

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

}

$myResult2 = eregi($myPattern, $testString); if ($myResult2)

{

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

}

else

{

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

}

?>

</body>

</html>

2.Save the code to C:\inetpub\wwwroot\PHP\EregEregiTest.php.

559

Chapter 23

3.Enter the URL http://localhost/PHP/EregEregiTest.php in Internet Explorer, and inspect the displayed results, as shown in Figure 23-8. When using ereg(), there is no match, as indicated by the first displayed message, and when using eregi(), there is a match, as indicated by the second displayed message.

Figure 23-8

How It Works

The pattern [A-Z][0-9] is assigned to the variable $myPattern. You can’t use the \d metacharacter to match a numeric digit because it is not recognized inside ereg():

$myPattern = ‘[A-Z][0-9]’;

The test string is a9, and it is assigned to the $testString variable:

$testString = “a9”;

The result of ereg() is assigned to the $myResult variable, with $myPattern as the first argument of ereg() and $testString the second. Thus, you are testing whether the pattern [A-Z][0-9] has a match in the test string a9:

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

If there is a match, $myResult contains the boolean value True (1). If there is no match, it contains the boolean value False (0). You use that boolean value to control what is displayed to the user:

if ($myResult)

The first character of the test string, a9, is not contained in the character class [A-Z], because matching is case sensitive.

When using ereg(), the $myResult variable returns False. Therefore, the message No match was found when testing case sensitively. is displayed:

else

{

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

}

560

PHP and Regular Expressions

However, when the eregi() function is used matching is case insensitive; so the first character of the test string, a9, is matches the character class [A-Z]. And the second character of the test string is matches the character class [0-9] because there is a match for each component of the pattern. The value contained in the variable $myResult2 is, therefore, the boolean value True.

Because the value contained in $myResult2 is True, the message A match was found when testing case insensitively. is displayed:

if ($myResult2)

{

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

}

The ereg_replace() Function

The ereg_replace() function attempts to match a pattern case insensitively. If a match is found, the match is replaced by the specified replacement text. If multiple matches exist, each is replaced.

The ereg_replace() function takes three arguments. The first argument is the pattern to be used in attempted matching. The second argument is the text used to replace any match. The third argument is the test string.

Try It Out

Using the ereg_replace() Function

1.In a text editor, enter the following code:

<html>

<head>

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

<body>

<?php

$myPattern = “Hello”; $myReplacement = “Hi”; $myString = “Hello world!”;

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

echo “<p>The replacement text is ‘$myReplacement’.</p>”; $replacedString = ereg_replace($myPattern, $myReplacement, $myString); $displayString = “<p>After replacement the string becomes: ‘ “; $displayString = $displayString . $replacedString . “‘.</p>”;

echo $displayString; ?>

</body>

</html>

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

3.Enter the URL http://localhost/PHP/ereg_replaceDemo.php in Internet Explorer, and inspect the displayed results, as shown in Figure 23-9. The character sequence Hello in the original sequence has been replaced by the character sequence Hi.

561

Chapter 23

Figure 23-9

How It Works

Three variables, $myPattern, $myReplacement, and $myString, are declared and assigned string values:

$myPattern = “Hello”; $myReplacement = “Hi”; $myString = “Hello world!”;

Then the original values of those variables are echoed to the user:

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

echo “<p>The replacement text is ‘$myReplacement’.</p>”;

The variable $replacedString is declared and assigned a value returned by the ereg_replace() function. Notice the order of the arguments of the ereg_replace() function; the test string is the third argument, not the second, as you might have expected from familiarity with the ereg() function:

$replacedString = ereg_replace($myPattern, $myReplacement, $myString);

Finally, the $displayString variable is declared. The value of $displayString is created by concatenating literal text and the value of $replacedString. Notice that the period character is used to concatenate strings in PHP:

$displayString = “<p>After replacement the string becomes: ‘ “; $displayString = $displayString . $replacedString . “‘.</p>”; echo $displayString;

If no match existed for the pattern, the original string would have been returned.

562