1
我有以下代码。preg_replace - 数组中的随机字
<?php
$user['username'] = 'Bastian';
$template = 'Hello {user:username}';
$template = preg_replace('/\{user\:([a-zA-Z0-9]+)\}/', $user['\1'], $template);
echo $template;
// Output:
// Notice: Undefined index: \1 in C:\xampp\htdocs\test.php on line 5
// Hello
我想,你知道我会做什么(我希望你知道)。 我尝试替换$ user ['$ 1'],$ user [“$ 1”]或$ user [$ 1],Nothing Works!
我希望你能帮助我=) 先谢谢你!
非常感谢您^^ <?php $ user ['userID'] ='1'; $ user ['username'] ='FooBar'; function repl($ m) { \t global $ user; \t return $ user [$ m [1]]; } $ template ='你好{user:username}'; echo preg_replace_callback('/ \ {user \:([a-zA-Z0-9] +)\} /','repl',$ template); //输出: //你好FooBar – Patrick 2012-04-28 12:46:46