如何在字符串中使用函数?

mon*_*oys 7 php string

'<a rel="nofollow" href="$1" class="bbc_link new_win" target="_blank">'
Run Code Online (Sandbox Code Playgroud)

我想使用这个urlencode()功能:

 '<a rel="nofollow" href="urlencode($1)" class="bbc_link new_win" target="_blank">'
Run Code Online (Sandbox Code Playgroud)

......但我不能用这个:

 '<a rel="nofollow" href="'.urlencode($1).'" class="bbc_link new_win" target="_blank">'
Run Code Online (Sandbox Code Playgroud)

...因为$1不是字符串中的变量; 它是一个简单的免费论坛中的元变量.

它发送 http://www.test.com/out.php?out=http://www.example.com

Oll*_*lie 6

这个疯狂的黑客怎么样?

<?
$_ = 'urlencode';
echo "<a rel=\"nofollow\" href=\"{$_($1)}\" class=\"bbc_link new_win\" target=\"_blank\">";
Run Code Online (Sandbox Code Playgroud)


Dan*_*jel 5

看看这个技巧:

function foo() { return "title"; }

$func = function($param) { return $param; };

$link = 'http://www.test.com/out.php?out=http://www.example.com';
echo "<a rel=\"nofollow\" href=\"{$func(urlencode($link))}\" class=\"bbc_link new_win\" target=\"_blank\">{$func(foo())}</a>";
Run Code Online (Sandbox Code Playgroud)

$func()将作为函数调用,并且括号中的表达式将像任何其他 PHP 代码一样进行计算。

黑客在这里找到


Dan*_*man 2

你不能这样做。您需要在此字符串中生成值或替换值的位置对其进行编码,而不是在您无权访问该值的模板中对其进行编码。