2015-09-10 33 views
0

我正在學習一些預製網站的PHP,所以我可以看到它是如何完成的,因爲我是一個視覺學習者,但我遇到了問題。preg_replace_callback():需要參數2是有效的回調

我得到了一個錯誤說

preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

現在我知道這是因爲不支持preg_replace()preg_replace_callback(),但在改變它一個較新的PHP的,我得到這個錯誤:

preg_replace_callback(): Requires argument 2, 'preg_replace('/(.{59})/', '\$1< >', '$1')', to be a valid callback

該代碼在聊天室中留下了太長的消息,分成兩個單獨的聊天室,以保留聊天窗口中的所有字母。

錯誤位於第20行,其中只有$msg);的立場。

if ($shoutbox['fixLongWords'] > 5 && $shoutbox['fixLongWords'] < $shoutbox['maxMsgLenght']) 
{ 
    $non_breaking_space = $context['utf8'] ? ($context['server']['complex_preg_chars'] ? '\x{A0}' : "\xC2\xA0") : '\xA0'; 

    if ($context['browser']['is_gecko'] || $context['browser']['is_konqueror']) 
     $breaker = '<span style="margin:0 -0.5ex 0 0"> </span>'; 
    elseif ($context['browser']['is_opera']) 
     $breaker = '<span style="margin:0 -0.65ex 0 -1px"> </span>'; 
    else 
     $breaker = '<span style="width:0;margin:0 -0.6ex 0 -1px"> </span>'; 

    $shoutbox['fixLongWords'] = (int) min(1024, $shoutbox['fixLongWords']); 

    if (strlen($msg) > $shoutbox['fixLongWords']) 
    { 
     $msg = strtr($msg, array($breaker => '< >', '&nbsp;' => $context['utf8'] ? "\xC2\xA0" : "\xA0")); 
     $msg = preg_replace(
      '~(?<=[>;:!? ' . $non_breaking_space . '\]()]|^)([\w\.]{' . $shoutbox['fixLongWords'] . ',})~e' . ($context['utf8'] ? 'u' : ''), 
      'preg_replace(\'/(.{' . ($shoutbox['fixLongWords'] - 1) . '})/' . ($context['utf8'] ? 'u' : '') . '\', \'\\$1< >\', \'$1\')', 
      $msg); 
     $msg = strtr($msg, array('< >' => $breaker, $context['utf8'] ? "\xC2\xA0" : "\xA0" => '&nbsp;')); 
    } 
} 

任何人都知道什麼可能會導致問題?

+1

「preg_replace_callback()」的第二個參數是一個匿名函數,例如, 'function(){...}'或函數名稱,例如''functionNameAsString「',然後你可以像我看到的一個普通函數 – Rizier123

回答

2

PHP文檔describes this function作爲

Perform a regular expression search and replace using a callback:

mixed preg_replace_callback (mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]])

callable類需要一個函數的名稱(字符串,例如'print_r')或實際功能function(){ // do something }

preg_replace_callback()進程會將匹配數組傳遞給「可調用函數」並返回一個字符串。從文檔描述第二個參數:

A callback that will be called and passed an array of matched elements in the subject string. The callback should return the replacement string. This is the callback signature:

string handler (array $matches)
+0

那樣定義。感謝您解釋這一點,我剛剛注意到,我的家用電腦上有一個較新的PHP版本,而不是在學校服務器上,所以我會離開它直到學校更新,如果問題恢復,我會問老師。感謝您的幫助:) – Katsu

+0

如果我可能會問,這兩個版本是什麼? – M1ke

相關問題