我有一段文字,例如:
XYZ id dui velerat bibendum hendrerit aliquet ac lorem。整数ABC
turpis, facilisis sat amet ultricies non, tempor LMNOP est. Aliquam
坐阿梅特智人胡斯托。 Quisque TUV dolor dolor, eu sodales mi.
我需要用编号占位符替换粗体字。
%%%1%%% id dui vel Erat bibendum hendrerit aliquet ac lorem。整数%%%2%%%
turpis, facilisis sat amet ultricies non, tempor %%%3%%% est. Aliquam
坐阿梅特智人胡斯托。 Quisque %%%4%%% dolor dolor, eu sodales mi.
进行此替换时,我需要将要替换的文本(例如 XYZ)存储到数组中,并键入占位符的编号(例如 1),以便稍后我可以返回并将它们与原始值交换。
困难的部分是我事先不知道替换文本,所以我需要一个回调来根据当前已替换的项目数生成它。
我正在旧版本的 PHP 上运行,并且我的工具包中没有可用的匿名函数,因此我相信我只能使用 preg_replace_callback
。看来我的问题归结为我需要我创建的函数(使用 < code>create_function)能够访问一些共享的全局状态——至少是一个共享的全局数组,我可以在其中推送和弹出替换到堆栈上的文本。
这可能吗?
I've got a piece of text, for example:
XYZ id dui vel erat bibendum hendrerit aliquet ac lorem. Integer ABC
turpis, facilisis sit amet ultricies non, tempor LMNOP est. Aliquam
sit amet sapien justo. Quisque TUV dolor dolor, eu sodales mi.
I need to replace the bolded words with numbered placeholders.
%%%1%%% id dui vel erat bibendum hendrerit aliquet ac lorem. Integer %%%2%%%
turpis, facilisis sit amet ultricies non, tempor %%%3%%% est. Aliquam
sit amet sapien justo. Quisque %%%4%%% dolor dolor, eu sodales mi.
Doing this replacement, I need to store into an array the text it's replacing (e.g. XYZ) and keyed to the number of the placeholder (e.g. 1), such that I can go back and swap them back out with the original values later on.
The difficult part is that I don't know the replacement text in advance, so I need a callback to generate it based on the current count of items that have been replaced already.
I am running on an older version of PHP and do not have anonymous functions available in my toolkit, so I believe I am limited to callbacks using preg_replace_callback
. It seems my problem boils down to the fact that I need the functions I create (using create_function
) to be able to access some shared global state -- at the very least a shared global array where I can push and pop text that was replaced onto the stack.
Is this possible?
发布评论
评论(3)
您会发现创建常规函数比使用
create_function()
更容易。 (不必处理所需的复杂字符串转义会更容易。)使用
global $var;
设置全局。然后在运行您的替换后访问该全局(如果替换调用在函数中,您还需要在那里说
global $var;
)。Instead of using
create_function()
you will find it easier to make a regular function. (It's just easier not to have to deal with the complicated string escaping needed.)Use
global $var;
to set a global.Then after running your replace access that global (if the replace call is in a function you will need to say
global $var;
there as well).您始终可以传入类方法并使用实例属性(只需查看 回调类型)。如果
global
关键字更适合您的情况,那么它也是一种可能。You can always pass in a class method and use the instances properties (just look at the callback type). The
global
keyword is a possibility as well if it fits your situation better.如果您使用 PHP 5.3,则可以创建一个闭包并在回调中导入本地范围的数据。这避免了全局数据的混乱,同时允许您的回调访问它需要的数据。请参阅以下简单示例
If you're using PHP 5.3, you can create a closure and import locally scoped data within the callback. This avoids the messiness of global data while allowing your callbacks to access the data that it needs. See the following for a trivial example