2009-09-21 2 views
0

이 간단한 코드를 몇 가지 코드가 있습니다 :preg_replace_callback에서 콜백 함수 지정?

function callback_func($matches) { 
    return $matches[0] . "some other stuff"; 
} 

function other_func($text) { 
    $out = "<li>"; 
    preg_replace_callback("/_[a-zA-Z]*/","callback_func",$desc); 
    $out .= $desc ."</li> \r\n"; 
    return $out; 
} 

echo other_func("This is a _test"); 

<li>This is a _testsome other stuff</li> 

하지만 그냥

<li>This is a _test</li> 

무엇을하고 있어요? 틀린/phiz 신을 달래기 위하여 무슨 기괴한 주문이 요구됩니까?

답변

5

preg_replace_callback 문자열을 수정하지 않고 대신 수정 된 복사본을 반환합니다. 다음 내용을 읽어보십시오.

function other_func($text) { 
    $out = "<li>"; 
    $out .= preg_replace_callback("/_[a-zA-Z]*/","callback_func",$desc); 
    $out .= "</li> \r\n"; 
    return $out; 
} 
+0

죄송합니다. 새로 고침해야합니다. 네가 한 직후에 그것을 알아 냈어. 어쨌든 고마워. –

0

문제는 함수의 출력을 $ out 변수에 추가하지 않는다는 것입니다.

$out .= $matches[0] . "some other stuff"; 

는 그 다음 출력에 당신을 위해 문자열로 결과를 추가합니다 : 그래서 callback_func에서()를 사용합니다. 그것이 그렇듯이, 당신은 가치를 반환하고 그것으로 아무 것도하지 않습니다.

+0

preg_replace_callback("/_[a-zA-Z]*/","callback_func",$desc); 

을 변경했다. '$ out'은'other_func'에 있지만'$ matches'는'callback_func'에 있습니다. –

0

설명해보십시오. preg_replace_callback은 원래의 제목을 수정하지 않았습니다. 나는 당신이 범위를 혼합하고

$desc = preg_replace_callback("/_[a-zA-Z]*/","callback_func",$desc);