0
$details = "text...[book=123]...text...";

$details = preg_replace_callback(
  "/\[book=(.+?)\]/smi",
  function ($m) {
      global $skip_books;
      $book = $m[1];  // 123
      $feed = $m[2];  // 456       
  return "<div id=\"view_book_".$book."_".$feed."\"></div>";
  },
  $details
);

With this pattern i can only get $book ($m[1]):

"/\[book=(.+?)\]/smi"`

But i want to get $feed ($m[2]) too, so i replace to this [book=123_456].

How to get "456" ($m[2]) after the underline?

"/\[book=(.+?)_(.+?)\]/smi" ???
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
richard
  • 1,456
  • 4
  • 15
  • 22
  • 1
    Please don't start your question with code, start with a short explanation what you are trying to achieve. – Fabian Schmengler Jan 25 '13 at 00:44
  • 1
    In your code, you don't show what/where the feed is in `$details`. And your option you put at the end would work with `[book=123_456]`, though you want to be as specific as possible in your regex as well, so if there are only numbers, replace `(.+?)` with `([0-9]+)` – Jon Jan 25 '13 at 00:48
  • @fab IS THIS REALLY A MATTER?? You should give me a hand to solve my problem. Not focus on the format of question and down vote me because of this! – richard Jan 25 '13 at 00:49
  • YES IT IS AND PLEASE DO NOT YELL IN THE LIBRARY – Fabian Schmengler Jan 25 '13 at 00:55
  • Mateys, stop yer infightin or I'll make ye walk the plank! =p – Ja͢ck Jan 25 '13 at 01:02

1 Answers1

2

Don't use global here; you're already using a closure, so use the use:

function ($m) use ($skip_books) {
    // ...
}

Btw, you're not actually using $skip_books in the code you've shown so far, but I'm assuming that's because you've simplified it

If your arguments are always numbers, don't use something generic like (.+?) but be specific (the more the better):

/\[book=(\d+)_(\d+)\]/i

I've also removed the /s and /m modifiers, which are useless here.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309