PHP如何在做preg_replace的同时做base64encode [英] php how to do base64encode while doing preg_replace

查看:95
本文介绍了PHP如何在做preg_replace的同时做base64encode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用preg_replace查找BBCODE并将其替换为HTML代码, 但是在执行此操作时,我需要base64encode网址,该怎么办?

I am using preg_replace to find BBCODE and replace it with HTML code, but while doing that, I need to base64encode the url, how can I do that ?

我正在这样使用preg_replace:

<?php
$bbcode = array('#\[url=(.+)](.+)\[/url\]#Usi');

$html = array('<a href="$1">$2</a>');

$text = preg_replace($bbcode, $html,$text);

如何base64encode href值,即$1吗?

我尝试做:

$html = array('<a href="/url/'.base64_encode('{$1}').'/">$2</a>');

,但其编码为{$1},而不是实际链接.

but its encoding the {$1} and not the actual link.

推荐答案

您可以使用

它将一个函数作为第二个参数.此函数会从您的正则表达式传递一个匹配项数组,并有望返回整个替换字符串.

It takes a function as the second argument. This function is passed an array of matches from your regular expression and is expected to return back the whole replacement string.

测试

这篇关于PHP如何在做preg_replace的同时做base64encode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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