Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

If PREG_PATTERN_ORDER is used, the first element of the matches array will
contain an array of all the matches against the full pattern. The other elements of the
array will contain arrays of matches against subpatterns.


If PREG_SET_ORDER is used, each element of the matches array contains an array
organized like those created by preg_match. The first element is the entire matching
string. Each subsequent element contains the match against the subpattern for that match.


string preg_quote(string text)


The preg_quote function returns text with backslashes inserted before character that
have special meaning to the functions in this section. The special characters are:


. \ + *? [ ^ ] $ ( ) { } =! < > | :


string preg_replace(string pattern, string replacement, string
text)


The preg_replace function is equivalent to ereg_replace. Each time the pattern
matches a substring within the text argument, it is replaced with the replacement
argument. The text argument is unchanged, but the altered version is returned.


If the pattern contains subexpressions in parentheses, the replacement argument may
contain a special code for specifying which subexpression to replace. The form is to use
two backslashes followed by a single digit, zero through nine. Zero matches the entire
expression; one through nine each match the first nine subexpressions, respectively.
Subexpressions are numbered left to right, which accounts for nested subexpressions.


<?


// swap newlines for break tags
$text = "line1\nline2\nline3\n";
print(preg_replace("|\n|", "
", $text));


print("


\n");


//mix up these words
$text = "one two three four";
print(preg_replace("|([a-z]+) ([a-z]+) ([a-z]+)
([a-z]+)|",
"\4 \2 \1 \3", $text));
?>

Free download pdf