函数preg_replace [英] preg_replace with function
问题描述
我有一些通过PHP运行的HTML:
I have some HTML that is being run through PHP:
<a href="?char=">&</a>
,而我想使用preg_replace
将其第一个&
替换为urlencode
d值.但是:
and I'm wanting to use a preg_replace
to replace the first &
with a urlencode
d value of it. However:
preg_replace("/char=\">(.*?)<\/a>/", "char=".urlencode("$1")."\">$1</a>", $links);
但是这给了我值$1
,而不是期望的反向引用.我该怎么做我要做的事情(使输出看起来像<a href="?char=%26">&</a>
)?
But this gives me the value $1
, instead of the expected back-reference. How can I do what I'm trying to do (make the output look like <a href="?char=%26">&</a>
)?
推荐答案
您可以在正则表达式中使用修饰符e
或使用功能preg_replace_callback
(请参阅文档).
You can use the modifier e
in your regexp or use the function preg_replace_callback
(see the doc) instead.
这篇关于函数preg_replace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!