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


If you execute this script, it will output the following:

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

As you can see, the $matches array contains an array whose elements are arrays that correspond to the matches found for each of the references. In this case, because no reference was specified, only the 0th element of the array is present, but it contains both the string "beautiful" and "beauty". By contrast, if you had executed this regex using preg_match(), only the word "beautiful" would have been returned.

Search-and-replace operations in the world of PCRE are handled by the preg_replace function:

preg_replace (pattern, replacement, string[, limit]);

Much like ereg_replace(), this function applies the regex pattern to string and then substitutes the placeholders in replacement with the references defined in it. The limit parameter can be used to limit the number of replacements to a maximum number. Here's an example, which will output marcot at tabini dot ca:

<?php

$s = 'marcot@tabini.ca';

echo preg_replace ('/^(\w+)@(\w+)\.(\w{2,4})/', '\1 at \2 dot \3', $s);

?>

Keep in mind that this is only one way of using preg_replace(), in which the entire input string is substituted by the replacement string. In fact, you can use this function to replace only small portions of text:

<?php

$s = 'The pen is on the table';

echo preg_replace ('/on/', 'over', $s);

?>

If you execute this script, preg_replace() will replace the word "on" with the word "over" in $s, resulting in the output The pen is over the table.

The last function that I want to bring to your attention is preg_split(), which is somewhat equivalent to the explode() function that we discussed earlier, with the difference that it takes a regular expression as a delimiter, rather than a straight string, and that it includes a few additional features:

preg_split (pattern, string[, limit[, flags]]);

The preg_split function works by breaking string in substrings delimited by sequences of characters delimited by pattern. The optional limit parameter can be used to specify a maximum number of splitting operations. The flags parameter, on the other hand, can be used to modify the behavior of the function as described in


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

Natural 422
Statistic


Last Post

 
Top! Top!