11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 9 • STRINGS AND REGULAR EXPRESSIONSwww.it-ebooks.infoIn 2010 the company celebrated skyrocketing revenues and expansion.Added in <strong>PHP</strong> 5.3.0, the preg_filter() function operates in a fashion identical to preg_replace(),except that, rather than returning the modified results, only the matches are returned.Creating a Custom Replacement FunctionIn some situations you might wish to replace strings based on a somewhat more complex set of criteriabeyond what is provided by <strong>PHP</strong>’s default capabilities. For instance, consider a situation where you wantto scan some text for acronyms such as IRS and insert the complete name directly following theacronym. To do so, you need to create a custom function and then use the functionpreg_replace_callback() to temporarily tie it into the language. Its prototype follows:mixed preg_replace_callback(mixed pattern, callback callback, mixed str[, int limit [, int count]])The pattern parameter determines what you’re looking for and the str parameter defines the stringyou’re searching. The callback parameter defines the name of the function to be used for thereplacement task. The optional parameter limit specifies how many matches should take place. Failingto set limit or setting it to -1 will result in the replacement of all occurrences. Finally, the optional countparameter will be set to the number of replacements made. In the following example, a function namedacronym() is passed into preg_replace_callback() and is used to insert the long form of variousacronyms into the target string:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!