" (" . $acronyms[$matches[1]] . ")";
else
return $matches[1];
}
// The target text
$text = "The
IRS offers tax forms in
PDF format on the
WWW.";
// Add the acronyms' long forms to the target text
$newtext = preg_replace_callback("/
(.*)<\/acronym>/U", 'acronym',
$text);
print_r($newtext);
?>
This returns the following:
The IRS (Internal Revenue Service) offers tax forms
in PDF (Portable Document Format) on the WWW (World Wide Web).
Splitting a String into Various Elements Based on a Case-Insensitive Pattern
The preg_split() function operates exactly like split(), except that pattern can also
be defined in terms of a regular expression. Its prototype follows:
array preg_split(string pattern, string string [, int limit [, int flags]])
If the optional input parameter limit is specified, only limit number of substrings
are returned. Consider an example:
248 CHAPTER 9 ?– STRINGS AND REGULAR EXPRESSIONS
$delimitedText = "Jason+++Gilmore+++++++++++Columbus+++OH";
$fields = preg_split("/\+{1,}/", $delimitedText);
foreach($fields as $field) echo $field."
";
?>
This returns the following:
Jason
Gilmore
Columbus
OH
?– Note Later in this chapter, the section titled ???Alternatives for Regular Expression Functions??? offers
several standard functions that can be used in lieu of regular expressions for certain tasks.
Pages:
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326