我有模式问题。 我需要处理一些文本,例如:解析器替换模式
<div>{text1}</div><span>{text2}</span>
的过程中,我需要的地方{文本1}和{}文本2从字典一些话来获取后。 我正在准备用于不同语言的ui界面。 这是用PHP编写的。现在我有这样的事情,但是,这并不工作:
function translate_callback($matches) {
global $dict;
print_r($matches);
return 'replacement';
}
function translate($input) {
return preg_replace_callback('/(\{.+\})/', 'translate_callback', $input);
}
echo translate('<div>{bodynum}</div><span>{innernum}</span>');
这是测试scenarion,但我无法找到定义模式的方式,因为这一个代码匹配
{bodynum}</div><span>{innernum}
但我想要匹配的图案
{bodynum} and then {innernum}
有人可以帮我解决这个问题。谢谢。
解决方案:
为了匹配之间{和}的任何字符,但不要贪 - 匹配与结束的最短串}(在停止+贪婪?)。
所以现在的模式看起来像:'/{(.+?)}/',而且是我想要的。
我解决了这个问题!匹配{和}之间的任何字符,但不要贪婪 - 匹配以}结尾的最短字符串(“停止+正在贪婪)”。 –