Jump to content

Preg_replace


johnnyk

Recommended Posts

I'm bad with terminology and what not, so if I confuse you and sound like an idiot, I warned you:

Is it possible to pass the search of a preg_replace() to a function via the replacement? What I mean is something like this:
[code]
<?php
function func($text){
   echo $text . ' is today's date';
}

$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = func('$1 $2, $3');
echo preg_replace($pattern, $replacement, $string);
?>
[/code]
I want it to echo "April 15, 2003 is today's date" but it's echoing "$1 $2, $3 is today's date". Yes, I know that I could just add "is today's date" to the replacement, but that's not the point. Ignoring what the function does, is it possible to use it via the replacement? It's a bad example but I think you understand what I mean.

Any ideas?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.