0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.