preg_replace_callback() 内存问题
我在测试查找/替换功能时遇到内存问题。
假设搜索主题是:
$subject = "我在A+杂志上写了一篇文章。 它很长而且充满了文字。 我想用链接替换本文中的每个 A+ 实例 到专门用于 A+ 的页面。
";
要查找的字符串:
$find='A+'; $find = preg_quote($find,'/');
替换函数回调:
function replaceCallback($match) { if (is_array($match)) { return '<a class="tag" rel="tag-definition" title="Click to know more about ' .stripslashes($match[0]) . '" href="?tag=' . $match[0]. '">' . stripslashes($match[0]) . '</a>'; } }
和调用:
$result = preg_replace_callback($find, 'replaceCallback', $subject);
现在,从数据库中提取完整的搜索模式。 到目前为止,它是:
$find = '/(?![^<]+>)\b(voice recognition|test project reference|test|synesthesia|Superflux 2007|Suhjung Hur|scripts|Salvino a. Salvaggio|Professional Lighting Design Magazine|PLDChina|Nicolas Schöffer|Naziha Mestaoui|Nabi Art Center|Markos Novak|Mapping|Manuel Abendroth|liquid architecture|LAb[au] laboratory for Architecture and Urbanism|l'Arca Edizioni|l' ARCA n° 176 _ December 2002|Jérôme Decock|imagineering|hypertext|hypermedia|Game of Life|galerie Roger Tator|eversion|El Lissitzky|Bernhard Tschumi|Alexandre Plennevaux|A+)\b/s';
然后在 7 个 mysql 表的 23 列中查找(如果找到则替换)这个 $find 模式。
使用建议的 preg_replace() 而不是 preg_replace_callback() 似乎已经解决了内存问题,但我遇到了新问题: preg_replace() 返回的主题缺少很多内容...
更新:
内容损失是由于使用 preg_quote($find,'/'); 现在它可以工作了,除了...“A+”在处理后变成“A”。
i'm having a memory issue while testing a find/replace function.
Say the search subject is:
$subject = "I wrote an article in the A+ magazine. It'\s very long and full of words. I want to replace every A+ instance in this text by a link to a page dedicated to A+.
";
the string to be found :
$find='A+'; $find = preg_quote($find,'/');
the replace function callback:
function replaceCallback($match) { if (is_array($match)) { return '<a class="tag" rel="tag-definition" title="Click to know more about ' .stripslashes($match[0]) . '" href="?tag=' . $match[0]. '">' . stripslashes($match[0]) . '</a>'; } }
and the call:
$result = preg_replace_callback($find, 'replaceCallback', $subject);
now, the complete searched pattern is drawn from the database. As of now, it is:
$find = '/(?![^<]+>)\b(voice recognition|test project reference|test|synesthesia|Superflux 2007|Suhjung Hur|scripts|Salvino a. Salvaggio|Professional Lighting Design Magazine|PLDChina|Nicolas Schöffer|Naziha Mestaoui|Nabi Art Center|Markos Novak|Mapping|Manuel Abendroth|liquid architecture|LAb[au] laboratory for Architecture and Urbanism|l'Arca Edizioni|l' ARCA n° 176 _ December 2002|Jérôme Decock|imagineering|hypertext|hypermedia|Game of Life|galerie Roger Tator|eversion|El Lissitzky|Bernhard Tschumi|Alexandre Plennevaux|A+)\b/s';
This $find pattern is then looked for (and replaced if found) in 23 columns across 7 mysql tables.
Using the suggested preg_replace() instead of preg_replace_callback() seems to have solved the memory issue, but i'm having new issues down the path: the subject returned by preg_replace() is missing a lot of content...
UPDATE:
the content loss is due to using preg_quote($find,'/');
It now works, except for... 'A+' which becomes 'A ' after the process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我正在尝试重现您的错误,但有一个解析错误需要首先修复。 要么这代码不足以成为一个好的示例,要么确实存在错误。
首先,您存储在 $find 中的值不是拉模式 - 所以我必须添加模式分隔符。
其次,您的替换字符串不包含锚标记的结束元素。
这段代码有效,但我不确定这是否是您想要的。 另外,我强烈怀疑您根本不需要 preg_replace_callback() 。
I'm trying to reproduce your error but there's a parse error that needs to be fixed first. Either this isn't enough code to be a good sample or there's genuinely a bug.
First of all, the value you store in $find is not a pull pattern - so I had to add pattern delimiters.
Secondly, your replace string doesn't include the closing element for the anchor tags.
This code works, but I'm not sure it's what you want. Also, I have have strong suspicion that you don't need preg_replace_callback() at all.
这对我有用,我不得不稍微改变一下预匹配,但它把我的每个 A+ 变成了一个链接。 您还缺少末尾的
。
This here works for me, i had to change the preg match a bit but it turns every A+ for me into a link. You also are missing a
</a>
at the end.好吧 - 我现在明白为什么你要使用回调
首先,我会将你的回调更改为此
stripslashes 命令不会对你有任何好处。
至于解决内存问题,您可能希望将模式分解为多个模式并在循环中执行它们。 我认为您的匹配对于 PHP 来说太大/太复杂,无法在单个调用周期中处理它。
Alright - I can see, now, why you're using the callback
First of all, I'd change your callback to this
The stripslashes commands aren't going to do you any good.
As far as addressing the memory issue, you may want to break down your pattern into multiple patterns and execute them in a loop. I think your match is just too big/complex for PHP to handle it in a single call cycle.