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:
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