preg_replace_callback()内存问题

时间:2009-03-31 14:15:07

标签: php regex preg-replace preg-replace-callback

我在测试查找/替换功能时遇到了内存问题。

说搜索主题是:

$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+.

";

要找到的字符串:

$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()返回的主题缺少很多内容...... < / p>

更新:

内容丢失是由于使用了preg_quote($ find,'/'); 它现在有效,除了......'A +'在过程之后变为'A'。

3 个答案:

答案 0 :(得分:2)

我正在尝试重现您的错误但是有一个解析错误需要先修复。要么这不足以成为一个好的样本,要么就是真正的错误。

首先,你在$ find中存储的值不是拉模式 - 所以我不得不添加模式分隔符。

其次,您的替换字符串不包含锚标记的结束元素。

$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+.
";

$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);

echo $result;

此代码有效,但我不确定这是你想要的。另外,我强烈怀疑你根本不需要preg_replace_callback()。

答案 1 :(得分:1)

这对我来说很有用,我不得不稍微改变一下preg的比赛,但是我把每个A +变成一个链接。你最后也错过了</a>

$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+.";

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("/A\+/", "replaceCallback", $subject);

echo $result;

答案 2 :(得分:0)

好吧 - 我现在可以看到你为什么要使用回调

首先,我将你的回调改为

function replaceCallback( $match )
{
    if ( is_array( $match ) )
    {
        $htmlVersion    = htmlspecialchars( $match[1], ENT_COMPAT, 'UTF-8' );
        $urlVersion     = urlencode( $match[1] );
        return '<a class="tag" rel="tag-definition" title="Click to know more about ' . $htmlVersion . '" href="?tag=' . $urlVersion. '">' . $htmlVersion  . '</a>';
    }
    return $match;
}

striplashes命令对你没有任何帮助。

就解决内存问题而言,您可能希望将模式分解为多个模式并在循环中执行它们。我认为您的匹配对于PHP来说太大/太复杂,无法在单个调用周期中处理它。