문제

찾기/교체 기능을 테스트하는 동안 메모리 문제가 있습니다.

검색 대상이 다음과 같이 가정합니다.

$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 개의 열에서 찾아서 (그리고 찾은 경우) 찾아냅니다.

preg_replace_callback () 대신 제안 된 preg_replace ()를 사용하여 메모리 문제를 해결 한 것으로 보이지만 경로 아래에 새로운 문제가 있습니다. preg_replace ()가 반환 한 주제는 많은 내용이 없습니다 ...

업데이트:

내용 손실은 preg_quote ($ find, '/')를 사용하기 때문입니다. 프로세스 후 '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가 단일 통화주기에서 처리하기에는 너무 크고 복잡하다고 생각합니다.

다른 팁

오류를 재현하려고하지만 먼저 고정 해야하는 구문 분석 오류가 있습니다. 이것은 좋은 샘플이되기에 충분한 코드가 아니거나 진정으로 버그가 있습니다.

우선, $ 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 ()가 필요하지 않다는 강한 의심이 있습니다.

이것은 나에게 효과가 있습니다. 나는 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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top