Jay
Jay

Reputation: 301

preg_replace variables

I've written the following preg_replace function:

function my_foo($bar) 
{
  $bar = preg_replace(
    '/\[blah=(.*)\](.*)(\[\/blah)\]/e', 
    '"<a href=\"http://blah.com/$1\" title=\"$2 On blah\">$2</a>"', 
    $bar);
  return $bar;
}

So anything wrapped in [blah=boob]Boo[/blah] turns into a link: blah.com/boob

Now I would like to take $1 and do something else with it, how would I go about using $1 or $2 from that preg_replace in other parts of my script?

As always, thank you for any feedback.

Upvotes: 0

Views: 767

Answers (2)

KingCrunch
KingCrunch

Reputation: 131881

preg_match() allows to set an optional parameter, that takes all matches.

Upvotes: 0

Simon
Simon

Reputation: 3539

You can use preg_replace_callback to call a custom function and pass the matches as array. In such a function, you can "export" those matches to access them from other parts of your script.

Upvotes: 1

Related Questions