preg_replace中的urlencode [英] urlencode in preg_replace
本文介绍了preg_replace中的urlencode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$str = preg_replace("'\(look: (.{1,80})\)'Ui",
"(look: <a href=\"dict.php?process=word&q=\\1\">\\1</a>)",$str);
我想对url进行编码,但是我该怎么做?
i want to encode url, but how can I do that?
我可以在preg_replace中使用urlencode()函数吗?
can i use urlencode() function in preg_replace?, something like that,
$str = preg_replace("'\(look: (.{1,80})\)'Ui",
"(look: <a href=\"dict.php?process=word&q=\\1\">\\1</a>)",$str);
您对在preg_replace中编码url有任何想法吗?
do you have any idea about encoding url in preg_replace?
推荐答案
You can use preg_replace_callback
, which allows you to produce the replacement string by running code directly:
$str = preg_replace_callback(
"'\(look: (.{1,80})\)'Ui",
create_function(
'$matches',
'return \'(look: <a href="dict.php?process=word&q='.urlencode($matches[1]).'">'.
$matches[1].'</a>)\';'
),
$str);
如果您使用PHP> = 5.3,则可以使上述操作减轻一些痛苦:
If you use PHP >= 5.3, you can make the above a bit less painful:
$str = preg_replace_callback(
"'\(look: (.{1,80})\)'Ui",
function($matches) {
return "(look: <a href=\"dict.php?process=word&q=".urlencode($matches[1])."\">".
$matches[1]."</a>)";
},
$str);
这篇关于preg_replace中的urlencode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文