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

PHP and Regular Expressions

The eregi_replace() Function

The eregi_replace() function matches in a case-insensitive way and replaces any match found in the test string using the replacement string.

The eregi_replace() function takes three arguments. The first is a string value that represents the regular expression pattern. The second is the replacement string that replaces each match found in the test string. The third argument is the test string.

Try It Out

Using the eregi_replace() Function

1.Enter the following code in a text editor:

<html>

<head>

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

<body>

<?php

$myPattern = “Doctor”; $myReplacement = “Dr.”;

$myString = “Doctor Smith spoke with another doctor.”; echo “<p>The original string was ‘$myString’.</p>”; echo “<p>The pattern is ‘$myPattern’.</p>”;

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

echo $displayString; ?>

</body>

</html>

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

3.In Internet Explorer, enter the URL http://localhost/PHP/eregi_replaceDemo.php, and inspect the displayed results, as shown in Figure 23-10. Notice that two occurrences of the word Doctor (one Doctor and the other doctor) have been replaced. In all likelihood, it would be undesirable to replace the second occurrence.

Figure 23-10

563

Chapter 23

How It Works

The example aims to replace all occurrences of the string Doctor, matched case insensitively, with the abbreviated form Dr..

You first assign values to the variables $myPattern, $myReplacement, and $myString:

$myPattern = “Doctor”; $myReplacement = “Dr.”;

$myString = “Doctor Smith spoke with another doctor.”;

After displaying the values of the preceding variables to the user, the eregi_replace() function is used to replace all occurrences of Doctor, matched case insensitively, with the form Dr.:

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

This causes the occurrences of Doctor and doctor (in the test string) to be replaced, because matching against the pattern Doctor is case insensitive. This reduces the specificity of matching because character sequences such as DocTOr, dOCtor, and so on would also be matched. In all likelihood, the replacing of doctor by Dr. is inappropriate. For this particular task, the case-sensitive matching of the ereg_replace() function would have been more appropriate. However, the example does demonstrate how case-insensitive matching works.

The split() Function

The split() function can split a string into component substrings, with the point where the string is split determined by a regular expression pattern. This can be useful when handling comma-separated values in files or handling dates that have different separators. The following example illustrates how the split() function can be used to split dates that use the hyphen, forward slash, and period character as separators.

The split() function can take two or three arguments. The first argument is the regular expression pattern at which a split is to take place. If a match is found, the character sequence that matches the pattern is discarded. This makes sense as default behavior when, for example, you have comma-separated data and want only the data and not the commas. The second argument is the test string. The third (optional) argument is an int value, which specifies the maximum number of times matching and splitting is to take place.

Try It Out

Using the split() Function

1.Type the following code into a code editor:

<html>

<head>

<title>split() Function Demo</title> </head>

<body>

<?php

564

PHP and Regular Expressions

$myString1 = “2004/09/23”; $myString2 = “2004.09.23”; $myString3 = “2004-09-23”; $myPattern = “[-/.]”;

list($year, $month, $day) = split($myPattern, $myString1);

echo “<p>String was: $myString1. <br />Year: $year <br />Month: $month <br />Day: $day</p>”;

list($year, $month, $day) = split($myPattern, $myString2);

echo “<br /><br /><p>String was: $myString2. <br />Year: $year <br />Month: $month <br />Day: $day</p>”;

list($year, $month, $day) = split($myPattern, $myString3);

echo “<br /><br /><p>String was: $myString3. <br />Year: $year <br />Month: $month <br />Day: $day</p>”;

?>

</body>

</html>

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

3.In Internet Explorer, type the URL http://localhost/PHP/splitdemo.php, and inspect the displayed results, as shown in Figure 23-11.

Figure 23-11

565

Chapter 23

How It Works

This example demonstrates how three strings, which represent dates, can be split using three possible separators: the forward slash, the period character, and the hyphen.

First, three variables are declared, each of which holds the date September 23, 2004. Each of the variables — $myString1, $myString2, and $myString3 — uses a different separator character.

$myString1 = “2004/09/23”; $myString2 = “2004.09.23”; $myString3 = “2004-09-23”;

Then the variable $myPattern is declared and is assigned a string that represents a character class containing the hyphen, the forward slash, and the period character. The hyphen character is the first character in the character class, so this is not specifying a character class range:

$myPattern = “[-/.]”;

A list is created, containing the component parts of the date represented in myString1. The forward slash in the character class [-/.] matches twice, so the string myString1 is split into three parts, which are represented in the list by $year, $month, and $day:

list($year, $month, $day) = split($myPattern, $myString1);

The variables $year, $month, and $day are output:

echo “<p>String was: $myString1. <br />Year: $year <br />Month: $month <br />Day:

$day</p>”;

The process of creating a list that contains the component parts of a string is repeated with the variables $myString2 (which uses a period character as separator) and $myString3 (which uses a hyphen as separator):

list($year, $month, $day) = split($myPattern, $myString2);

echo “<br /><br /><p>String was: $myString2. <br />Year: $year <br />Month: $month <br />Day: $day</p>”;

list($year, $month, $day) = split($myPattern, $myString3);

echo “<br /><br /><p>String was: $myString3. <br />Year: $year <br />Month: $month <br />Day: $day</p>”;

The spliti() Function

The spliti() function does the same thing as the split() function, except that it matches case insensitively. If an alphabetic character is used as a separator, spliti() will match case insensitively, while split() matches case sensitively. However, because the most likely separators used are the comma, the hyphen, the forward slash, the semicolon, and the period character, the two functions will often behave identically in practical use.

The spliti() function, like the split() function, takes two or three arguments. The first argument is the regular expression pattern at which splitting is to take place. The second argument is the test string. The third (optional) argument is an int value, which specifies the maximum number of times matching

566