Re: Regular expressions
| From: | Jochem Maas | Date: | Tue, 03 Feb 2004 23:00:34 +0000 |
| Subject: | Re: Regular expressions | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-176740@lists.php.net to get a copy of this message | ||
replacement is evaluated... therefore:
function check_this_out($arg) {
return "<a href='#'>$arg</a>";} $myString = 'hello'; $pattern = '/(.*)/e'; $replacement = "check_this_out('\\1' . ' $myString')"; echo preg_replace($pattern, $replacement, 'test says:'); pete@doberad.com wrote:
Thank you very much Jochem and John. It works like a charm now, and I was beginning to grow weary after experimenting with result sets for almost 10 hours now. How would you - by the way - concatenate text to the replacement string? ;-Pete On Tue, 3 Feb 2004 14:15:00 -0500, holmes072000@charter.net (John W. Holmes) wrote:From: "Jochem Maas" <jochem@iamjochem.com>alternatively (actually this looks like the easier way to do it!) use preg_replace(), with the 'e' modifier tagged onto the end of the replacement expression: the 'e' modifier causes the expression to be evaluated as PHP. http://nl2.php.net/manual/en/function.preg-replace.php http://nl2.php.net/manual/en/pcre.pattern.modifiers.php something like: $pattern = '/\[link="([[:graph:]]+)"]/'; $replacement = "/check_this_out('\1')/e"; $output = preg_replace($pattern, $replacement, $output);Good idea, except the "e" modifier goes in the pattern, not the replacement. And you don't need delimiters in the replacement. $pattern = '/\[link="([[:graph:]]+)"]/e'; $replacement = "check_this_out('\1')"; $output = preg_replace($pattern, $replacement, $output); ---John Holmes...