preg_replace至preg_replace_callback与数组作为替代 [英] preg_replace to preg_replace_callback with an array as replacement

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

问题描述

我移植应用程序中使用的 PHP 5.5 的工作,我需要从preg_replace功能删除/ E的性格,要做到这一点,我使用preg_replace_callback ()

I'm migrating an application to work with PHP 5.5, and I need to remove the /e character from the preg_replace function, to do that I'm using preg_replace_callback()

我的实际功能是这样的:

My actual function looks like this:

preg_replace ( '#\{([a-z0-9\-_]*?)\}#Ssie' , '( ( isset($array[\'\1\']) ) ? $array[\'\1\'] : \'\' );' , $template );

其中:

$模板包含像这样的标记的html文件: {} USER_NAME

$template contains a html document with tags like this one: {user_name}

$阵列包含

$array['user_name'] = 'The user';

我一直在试图转换这与PHP 5.5工作,并没有成功。

I've been trying to convert this to work with PHP 5.5 with not success.

这是我做过什么至今:

return preg_replace_callback ( '#\{([a-z0-9\-_]*?)\}#Ssi' , function ( $array ) {
    return ( ( isset ( $array[1] ) ) ? $array[1] : '' );
} , $template );

但它不工作。 的标签括号关闭时不被取代

我想什么?

推荐答案

您需要使用使用关键字 $阵列拉进入你的匿名函数...

You need to use the use keyword to pull in $array into your anonymous function...

return preg_replace_callback ( '#\{([a-z0-9\-_]*?)\}#Ssi' , function ($matches) use ($array) {
    return ( ( isset ( $array[$matches[1]] ) ) ? $array[$matches[1]] : '' );
} , $template );

这篇关于preg_replace至preg_replace_callback与数组作为替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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