1

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

假设搜索主题是:

$主题 = "

我在 A+ 杂志上写了一篇文章。 它很长,充满了文字。 我想用链接替换本文中的每个 A+ 实例 到 A+ 专用页面。

";

要找到的字符串:

$查找='A+';
$find = preg_quote($find,'/');

替换函数回调:

函数replaceCallback($match)
    {
      if (is_array($match)) {
          return '<a class="tag" rel="tag-definition" title="点击了解更多关于 ' .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 '。

4

3 回答 3

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() 。

于 2009-03-31T14:39:22.877 回答
1

这对我有用,我不得不稍微改变一下预赛,但它把我的每个 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;
于 2009-03-31T14:31:47.687 回答
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;
}

stripslashes 命令对你没有任何好处。

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

于 2009-03-31T16:09:41.397 回答