This works much like the optional input
parameter regs in the function ereg(), except that the substrings are referenced using
238 CHAPTER 9 ?– STRINGS AND REGULAR EXPRESSIONS
backslashes, such as \0, \1, \2, and so on, where \0 refers to the entire string, \1 the first
successful match, and so on. Up to nine back references can be used. This example
shows how to replace all references to a URL with a working hyperlink:
$url = "Apress (http://www.apress.com)";
$url = ereg_replace("http://([a-zA-Z0-9./-]+)([a-zA-Z/]+)",
"
\\0", $url);
echo $url;
// Displays Apress (
http://www.apress.com)
?– Note Although ereg_replace() works just fine, another predefined function named str_replace()
is actually much faster when complex regular expressions are not required. str_replace() is
discussed in the later section ???Replacing All Instances of a String with Another String.???
Replacing Text in a Case-Insensitive Fashion
The eregi_replace() function operates exactly like ereg_replace(), except that the
search for pattern in string is not case sensitive. Its prototype follows:
string eregi_replace(string pattern, string replacement, string string)
Splitting a String into Various Elements Based on a Case-Sensitive Pattern
The split() function divides a string into various elements, with the boundaries
of each element based on the occurrence of a defined pattern within the string.
Pages:
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316