将标签更改为图库

I need to replace tag [gallery-1] in variable from database to actual gallery. Problem is, that that gallery is stated by that number in tag (i have multiple galleries or categories whatever). I tried str_replace, but it doesn't solve problem with number.
I need to take that number, select images from gallery number X and than echo them (replace with content depending on that number).

If you want to get the integer from the string [gallery-1] (including the brackets) try this :

$gallery = '[gallery-1]';
echo preg_replace('/\[gallery-(\d+)\]/', '$1', $gallery);

preg_match_all() is your friend:

$code = "<p>Test...test...test...</p> [gallery-1]";

preg_match_all("/\[gallery-(\d+)\]/", $code, $matches);

$galleries = array();

foreach ($matches[1] as $id) {
  $galleries[$id] = // Somehow load a gallery basing on id;
}

foreach ($galleries as $id => $c) {
  $code = str_ireplace("[gallery-" . $id . "]", $c, $code);
}

echo $code;

If you are still confised, here's an example.