note 11899 deleted from ref.pcre by sniper
| From: | sniper@php.net | Date: | Tue, 30 Sep 2003 12:17:33 +0000 |
| Subject: | note 11899 deleted from ref.pcre by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57409@lists.php.net to get a copy of this message | ||
Note Submitter: bigfart@asia.com
----
Ever wondered how to use a function within a PCRE? here's how:
$content = preg_replace("/\*(\w+)\*/e", "lookupImageCode($1)", $content);
This code replaces all instances of *text*, where text is a code for an image, with the output of
function lookupImageCode(). The /e and the quotes around the function name and arguments are
essential to make this work :-)
I use this on one of my websites to query a database and cross-reference image filenames with
special codes input by press release writers in our client's company. They type the code of an
image (displayed in a catalogue onscreen on the site's backend) and this little wonder of code
changes it to an img src tag on the fly as the page is loaded.
Enjoy!
p.s. thanks to the guys on #php who helped sort this out at 4am in the morning my time! (donut,
jfk, cardinal) Contact me if you like.