函数preg_replace [英] preg_replace with function

查看:100
本文介绍了函数preg_replace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些通过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 urlencoded 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆