Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Perl-Compatible Regular Expressions (PCRE)
 
Article Index
Perl-Compatible Regular Expressions (PCRE)
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9
Page 10


As in the case of ereg(), this function causes the regular expression stored in pattern to be matched against string, and any references matches are stored in matches. The optional flags parameter can actually contain only the value PREG_OFFSET_CAPTURE. If this parameter is specified, it will cause preg_match() to change the format of matches so that it will contain both the text and the position of each reference inside string. Let's make an example:

<?php

$s = 'Another beautiful day';

preg_match ('/beautiful/', $s, $matches, PREG_OFFSET_CAPTURE);

var_dump ($matches);

?>

If you execute this script, you should receive the following output:

array(1) {
[0]=>
array(2) {
[0]=>
string(9) "beautiful"
[1]=>
int(8)
}
}

As you can see, the $matches array now contains another array for each reference. The latter, in turn, contains both the string matched and its position within $s.

Another function of the PCRE family is preg_match_all, which has the same syntax as preg_match(), but searches a string for all the occurrences of a regular expression, rather than for a specific one. Here's an example:

<?php

$s = 'A beautiful day and a beauty of a lake';

preg_match_all ('/beaut[^ ]+/', $s, $matches);

var_dump ($matches)

?>

Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Your Ad Here
Mobile Wallpaper 97
Statistic


Last Post

 
Top! Top!