我有一个存储器问题而测试查找/替换功能。

说出搜索主题是:

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

此$发现模式是随后在23列跨越7个MySQL表寻找(和替换如果找到)。

使用建议的preg_replace()代替preg_replace_callback()似乎已经解决了内存问题,但我有向下的路径新问题:由preg_replace函数返回的主题()丢失了很多内容...

更新:

含量损失是由于使用preg_quote($发现, '/'); 现在它工作,除了...“A +”成为“A”的处理之后。

有帮助吗?

解决方案

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

首先,我要你的回调改变这一

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来处理它在单个调用周期。

其他提示

我试图重现你的错误,但有需要先固定一个解析错误。任一这是不够的代码是一个很好的样品或真正有一个错误。

首先,你在$查找存储的值不是拉模式 - 所以我不得不添加的图案分隔符

其次,您的替换字符串不包括用于锚定的标签的封闭元件。

$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()在所有。

在这里,这对我的作品,我不得不改变预浸比赛一点,但事实证明每一个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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top