Приглашаем посетить
Тютчев (tutchev.lit-info.ru)

Using Perl-Compatible Regular Expressions

Previous
Table of Contents
Next

Using Perl-Compatible Regular Expressions

preg_match('/php ((\d)\.\d\.\d+)/i'


Matching patterns in PCRE is done using preg_match() if only one occurrence is searched for, or preg_match_all() if multiple occurrences may exist. The syntax is the same as for ereg(): first the pattern, then the string, then the resulting array. However, the way the pattern is provided is slightly different. You need delimiters; most of the time slashes are used. After the delimiter, you can provide further instructions. Instruction g lets the search be done globally (for multiple matches), whereas instruction i deactivates case sensitivity.

Searching in Strings Using PCRE (preg_match.php)
<?php
  $string = 'This site runs on PHP ' . phpversion();
  preg_match('/php ((\d)\.\d\.\d+)/i',
    $string, $matches);
  vprintf('Match: %s<br /> Version: %s; Major: %d.',
    $matches);
?>

The function preg_match_all() works exactly the same; however, the resulting array is a multi-dimensional one. Each entry in this array is an array of matches as it would have been returned by preg_match(). The following code shows this.

Finding Multiple Matches in Strings Using PCRE (preg_match_all.php)
<?php
  $string = 'This site runs on PHP ' . phpversion();
  preg_match('/php ((\d)\.\d\.\d+)/i',
    $string, $matches);
  vprintf('Match: %s<br /> Version: %s; Major: %d.',
    $matches);
?>


Previous
Table of Contents
Next