说,字符串是:
$str="abcdefg foo() hijklmopqrst";
如何让php调用foo()并将返回字符串插入该字符串?
非常不愉快。您是否有一个允许此操作的函数列表,或者一般的函数列表?(同样,您也考虑了这一点对安全性和性能的影响,是吗?)
您可以使用eval(),但并不是广泛推荐的。
请给我们更多关于您的代码的信息。这根绳子是从哪里来的?你提到一个API?
这是:stackoverflow.com/questions/1005857/…
请不要把已经回答的问题改成另一个问题。打开一个新问题。
stackoverflow.com/q/3822839/1066234副本
如果你是打电话的方法的一些类,你可以使用正常的可变扩建。例如: P / < >
class thingie {
public function sayHello() {
return"hello";
}
}
$t = new thingie();
echo"thingie says: {$t->sayHello()}";
这将输出: P / < >
thingie says: hello
注意,braces周围的召唤,是需要的。 P / < >
但是这样调用一个全局函数似乎行不通
通过将要支持的全局函数包装在方法对象中,可以控制暴露于错误和安全性泄漏的足迹。我认为你很难找到一种更简单的方法来/安全地/提供这种力量。如果需要,您的包装方法甚至可以执行额外的验证/限制。
就用这个: P / < >
$str ="abcdefg".foo()."hijklmnopqrstuvwxyz";
它将呼叫功能期间创作的字符串。 P / < >
API只是给了我一个字符串,所以不能形成这种格式。
您想使用什么功能?
@爱情春天这是一个可怕的安排。您使用的是什么API来实现这一点?
我可以调用但不能修改其他人的代码,他没有给我一个API,我必须钩住$str来注入我的代码。
@爱泉在清澈??
$str="abcdefg foo() hijklmopqrst";
function foo() {return"bar";}
$replaced = preg_replace_callback("~([a-z]+)\(\)~",
function ($m){
return $m[1]();
}, $str);
输出: P / < >
$replaced == 'abcdefg bar hijklmopqrst';
这将允许任何情况下世纪文学功能的名称。如果你需要的任何其他symbols,给他们的pattern,公元前[a-zA-Z_]。 P / < >
很小心,功能允许你去叫。你应该至少检查,如果美元米[ 1 ] contains的whitelisted功能到不允许remote代码attacks溶液注射。 P / < >
$allowedFunctions = array("foo","bar" /*, ...*/);
$replaced = preg_replace_callback("~([a-z]+)\(\)~",
function ($m) use ($allowedFunctions) {
if (!in_array($m[1], $allowedFunctions))
return $m[0]; // Don't replace and maybe add some errors.
return $m[1]();
}, $str);
testrun C "abcdefg foo() bat() hijklmopqrst"outputs "abcdefg bar bat() hijklmopqrst"。 P / < >
optimisation为whitelisting进场(建筑pattern dynamically从allowed功能名称,公元前(foo|bar)。 P / < >
$allowedFunctions = array("foo","bar");
$replaced = preg_replace_callback("~(".implode("|",$allowedFunctions).")\(\)~",
function ($m) {
return $m[1]();
}, $str);
如果函数有参数呢?
function foo()
{
return 'Hi';
}
$my_foo = 'foo';
echo"{$my_foo()}";
这是一个很好的"技巧",您甚至可以将参数或其他变量传递给它,例如echo"{$my_foo('bar')}"或echo"{$my_foo($bar)}",在使用许多转义值构建SQL查询时尤其有用。
哇,太棒了!
$foo = foo();
$str ="abcdefg {$foo} hijklmopqrst";
我必须在$str时间内调用foo()。
@你的意思是在以后的某个时候?也许你在找eval($str)。
得到arbitrary expressions被evaluated从双上市的字符串,你可以在speculate可变-功能: P / < >
// A user function
function foo() {
return 'bar';
}
/**
* The hack
*
* @param $v mixed Value
* return mixed Value (untouched)
*/
$_ = function ( $v ) {
return $v;
};
// Happy hacking
echo"Foo is {$_( foo() )} and the sum is {$_( 41 + 1 )}...{$_( str_repeat( ' arrh', 3 ) )}!";
结果: P / < >
Foo is bar and the sum is 42... arrrh arrrh arrrh!
references: P / < >
https:/ / / / / / functions.variable-functions.php EN secure.php.net手册
secure.php.net https:/ / / / / /它language.types.string.php # language.types.string.parsing手册
它仍然是不可能的,有hacks可用,但不是什么我会推荐而suggest粘到与老式的公元前$str="abcdefg". foo() ." hijklmopqrst";点算子 P / < >
每级的复杂(卷曲)语法文件 P / < >
Note:
Functions, method calls, static class variables, and class constants inside {$} work since PHP 5. However, the value accessed will be interpreted as the name of a variable in the scope in which the string is defined. Using single curly braces ({}) will not work for accessing the return values of functions or methods or the values of class constants or static class variables.