0

我正在尝试创建一个钩子,以便在文本中找到以下内容时:

level,2

它用 , 替换它$skill_level * $n,逗号后面的数字是$n

这是我所拥有的:

$skill_level = 2;

$row['s_effect'] = "Long string of text that contains level,2."

preg_replace("/level,$n/",$skill_level * $n,$row['s_effect'])

我得到的结果是:

Long string of text that contains 02.

我想要一个结果:

Long string of text that contains 4.

由于某种原因,计算似乎"$skill_level * $n" (2 * 2)不起作用。

4

1 回答 1

3

你需要使用preg_replace_callback

$skill_level = 2;
$text = "Long string of text that contains level,2.";

$ret = preg_replace_callback("/level,(\d+)/", function ($matches) use ($skill_level) {
  return $matches[1] * $skill_level;
}, $text);
于 2012-09-25T04:02:33.643 回答