Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
PHP Programming With MySQL Second Edition.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
43.07 Mб
Скачать

CHAPTER 3

Manipulating Strings

can either use the escape character before the forward slash (\/) or

choose another valid character that is not part of the pattern.

Regular expression patterns consist of literal characters and

metacharacters, which are special characters that define the pattern

matching rules in a regular expression. Table 3-3 lists the metacharac-

ters that you can use with PCRE.

Metacharacter

.

\

^

$

()

[]

[^]

-

|

162

Description

Matches any single character

Identifies the next character as a literal value

Anchors characters to the beginning of a string

Anchors characters to the end of a string

Specifies required characters to include in a pattern match

Specifies alternate characters allowed in a pattern match

Specifies characters to exclude in a pattern match

Identifies a possible range of characters to match

Specifies alternate sets of characters to include in a

pattern match

Table 3-3

PCRE metacharacters

Matching Any Character

You use a period (.) to match any single character in a pattern.

A period in a regular expression pattern specifies that the pattern

must contain a value where the period is located. For example, the

following code specifies that the $ZIP variable must contain five

characters. Because the variable only contains three characters, the

preg_match() function returns a value of 0.

$ZIP = "015";

preg_match("/...../", $ZIP); // returns 0

In comparison, the following preg_match() function returns a value

of 1 because the $ZIP variable contains five characters:

$ZIP = "01562";

preg_match("/...../", $ZIP); // returns 1

Because the period only specifies that a character must be included in

the designated location within the pattern, you can include additional

characters within the pattern. The following preg_match() function

returns a value of 1 because the $ZIP variable contains the required

five characters along with the ZIP+4 characters.

Working with Regular Expressions

$ZIP = "01562-2607";

preg_match("/...../", $ZIP); // returns 1

Matching Characters at the Beginning

or End of a String

The ^ metacharacter anchors characters to the beginning of a string,

and the $ metacharacter anchors characters to the end of a string.

An anchor specifies that the pattern must appear at a particular posi-

tion in the string. To specify an anchor at the beginning of a line, the

pattern must begin with the ^ metacharacter. The following example

specifies that the $URL variable begin with http. Because the variable

does begin with "http", the preg_match() function returns 1.

$URL = "http://www.dongosselin.com";

preg_match("/^http/", $URL); // returns 1

163

All literal characters following the ^ metacharacter in a pattern com-

pose the anchor. This means that the following example returns 0

because the $URL variable does not begin with "https" (only "http"

without the s), as is specified by the anchor in the pattern:

$URL = "http://www.dongosselin.com";

preg_match("/^https/", $URL); // returns 0

To specify an anchor at the end of a line, the pattern must end with

the $ metacharacter. The following demonstrates how to specify that

a URL end with com:

$Identifier = "http://www.dongosselin.com";

preg_match("/com$/", $Identifier); // returns 1

The preceding code returns 1 because the URL assigned to the

$Identifier variable ends with com. However, the following code

returns 0 because the URL assigned to the $Identifier variable does

not end with gov:

$Identifier = "http://www.dongosselin.com";

preg_match("/gov$/", $Identifier); // returns 0

Matching Special Characters

To match any metacharacters as literal values in a regular expres-

sion, escape the character with a backslash. For example, a period (.)

metacharacter matches any single character in a pattern. If you want

to ensure that a string contains an actual period and not the metacha-

racter, you need to escape the period with a backslash. The top-level

domain in the following code is appended to the domain name with a

comma instead of a period. However, the regular expression returns 1

because the period in the expression is not escaped.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]