开发者

preg_replace variables

开发者 https://www.devze.com 2023-01-28 12:24 出处:网络
I\'ve written the following preg_replace function: function my_foo($bar) { $bar = preg_replace( \'/\\[blah=(.*)\\]开发者_JAVA技巧(.*)(\\[\\/blah)\\]/e\',

I've written the following preg_replace function:

function my_foo($bar) 
{
  $bar = preg_replace(
    '/\[blah=(.*)\]开发者_JAVA技巧(.*)(\[\/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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消