1

我有问题,能帮帮我吗?我有文本,我想将它从“替换为”。

<span>new var[128]; format(var, sizeof(var), "something %s %f", "Stuneris", 1589.0587); SendClientMessage(playerid, 0xFFFFFFFF, var);</span>

我想要"something %s %f""Stuneris"替换<span class="numb">"something %s %f"</span><span class="numb">"Stuneris"</span>

另一个问题是将十六进制代码从 替换0xFFFFFFFF<span class="numb">0xFFFFFFFF</span>

我需要做什么?

4

2 回答 2

1

你可以用preg_replace做到这一点:

preg_replace('/"([^"]*)"/', '<span class="numb">"\1"</span>', $string);

要匹配十六进制代码:

preg_replace('/0x([0-9A-Fa-f]*)/', '<span class="numb">\1</span>', $string);
于 2013-01-18T18:49:25.443 回答
1

替换十六进制代码很容易:

$text = preg_replace('/(0x[0-9a-f]+)/i','<span class="numb">$1</span>',$text);

替换引号内的字符串有点棘手。主要问题是 inlcude "something %s %f",但 exclude ", "(它分隔带引号的字符串,但也看起来像带引号的字符串)。如果您可以确定任何字符串中都不会有逗号,您可以使用:

$text = preg_replace('/("[^",]+")/i','<span class="numb">$1</span>',$text);

如果您必须允许逗号,这将排除所有以逗号开头的字符串:

$text = preg_replace('/("(?!,)[^"]+")/i','<span class="numb">$1</span>',$text);
于 2013-01-18T18:50:51.020 回答