2016-01-30 41 views
3

我曾經有這個代碼塊來模擬某種的BBCode的棄用:preg_replace函數的/ e修飾符使用數組作爲圖案和替換

$pattern = array(
    '/\\n/', 
    '/\\r/', 
    '/\[list\](.*?)\[\/list\]/ise', 
    '/\[b\](.*?)\[\/b\]/is', 
    '/\[strong\](.*?)\[\/strong\]/is', 
    '/\[i\](.*?)\[\/i\]/is', 
    '/\[u\](.*?)\[\/u\]/is', 
    '/\[s\](.*?)\[\/s\]/is', 
    '/\[del\](.*?)\[\/del\]/is', 
    '/\[url=(.*?)\](.*?)\[\/url\]/ise', 
    '/\[email=(.*?)\](.*?)\[\/email\]/is', 
    '/\[img](.*?)\[\/img\]/ise', 
    '/\[color=(.*?)\](.*?)\[\/color\]/is', 
    '/\[font=(.*?)\](.*?)\[\/font\]/ise', 
    '/\[bg=(.*?)\](.*?)\[\/bg\]/ise', 
    '/\[size=(.*?)\](.*?)\[\/size\]/ise' 
); 

$replace = array(
    '<br/>', 
    '', 
    '$this->sList(\'\\1\')', 
    '<b>\1</b>', 
    '<strong>\1</strong>', 
    '<i>\1</i>', 
    '<span style="text-decoration: underline;">\1</span>', 
    '<span style="text-decoration: line-through;">\1</span>', 
    '<span style="text-decoration: line-through;">\1</span>', 
    '$this->urlfix(\'\\1\',\'\\2\')', 
    '<a href="mailto:\1" title="\1">\2</a>', 
    '$this->imagefix(\'\\1\')', 
    '<span style="color: \1;">\2</span>', 
    '$this->fontfix(\'\\1\',\'\\2\')', 
    '$this->bgfix(\'\\1\',\'\\2\')', 
    '$this->sizefix(\'\\1\',\'\\2\')' 
); 

return preg_replace($pattern, $replace, nl2br(stripslashes($string))); 

但我移動到PHP 5.5,我收到錯誤在這裏,它曾經很好地工作,這是錯誤回報我得到:

推薦使用:的preg_replace():本/ e修飾符已過時,使用 preg_replace_callback相反在

我嘗試了幾個東西,但迄今沒有任何工作。

這是我試過到目前爲止代碼:

return preg_replace_callback(
    $pattern, 
    function($matches) use ($replace) { 
     return ((isset($replace[$matches[0]])) ? $replace[$matches[0]] : ''); 
    }, 
    nl2br(stripslashes($string)) 
); 

我一直在讀左右,但大多數的例子都涉及到基本替換,在這裏我有兩個數組。

通知有一些方法正在從$ replace區域中調用。

我該如何解決這個問題?這是正確的方法嗎?

+0

轉到http://stackoverflow.com/questions/21334934/deprecated-preg-replace-the-e-modifier-is-deprecated-use-preg-replace-call鏈接 – WisdmLabs

+0

我有點麻煩理解你試圖做什麼。是否你試圖在'use'部分傳遞兩個數組? – Quixrick

+0

不,它只是一個數組。我只是期待着和以前一樣的結果。 – Lucas

回答

0

經過大量的工作和測試和閱讀後,我提出了一個解決方案,我希望它可以幫助別人。

這裏是我的解決更換設置高亮

$bbcodes = [ 
    '/\\n/' => '$this->setLineJump()', 
    '/\\r/' => '$this->setReturn()', 
    '/\[list\](.*?)\[\/list\]/is' => '$this->setList(\'\\1\')', 
    '/\[b\](.*?)\[\/b\]/is' => '$this->setBold(\'\\1\')', 
    '/\[strong\](.*?)\[\/strong\]/is' => '$this->setBold(\'\\1\')', 
    '/\[i\](.*?)\[\/i\]/is' => '$this->setItalic(\'\\1\')', 
    '/\[u\](.*?)\[\/u\]/is' => '$this->setUnderline(\'\\1\')', 
    '/\[s\](.*?)\[\/s\]/is' => '$this->setStrike(\'\\1\')', 
    '/\[del\](.*?)\[\/del\]/is' => '$this->setStrike(\'\\1\')', 
    '/\[url=(.*?)\](.*?)\[\/url\]/is' => '$this->setUrl(\'\\1\',\'\\2\')', 
    '/\[email=(.*?)\](.*?)\[\/email\]/is' => '$this->setEmail(\'\\1\',\'\\2\')', 
    '/\[img](.*?)\[\/img\]/is' => '$this->setImage(\'\\1\')', 
    '/\[color=(.*?)\](.*?)\[\/color\]/is' => '$this->setFontColor(\'\\1\',\'\\2\')', 
    '/\[font=(.*?)\](.*?)\[\/font\]/is' => '$this->setFontFamiliy(\'\\1\',\'\\2\')', 
    '/\[bg=(.*?)\](.*?)\[\/bg\]/is' => '$this->setBackgroundColor(\'\\1\',\'\\2\')', 
    '/\[size=(.*?)\](.*?)\[\/size\]/is' => '$this->setFontSize(\'\\1\',\'\\2\')' 
]; 

$string = stripslashes($string); 

foreach ($bbcodes as $bbcode => $html) { 
    $string = preg_replace_callback(
     $bbcode, 
     function($matches) use($html) { 
      return $this->getBbCode($matches, $html); 
     }, 
     $string 
    ); 
} 

private function getBbCode($matches, $replace) 
{ 
    if (isset($matches[1])) { 

     $replacements = [ 
      '\1' => isset($matches[1]) ? $matches[1] : '', 
      '\2' => isset($matches[2]) ? $matches[2] : '' 
     ]; 

     return eval('return ' . strtr($replace, $replacements) . ';'); 
    } else { 

     return eval('return ' . $replace . ';'); 
    } 
} 

正如你可以看到我使用的對象,但你可以通過自己的函數替換它們。這裏真正重要的是你可以用元素替換元素,只需在數組中進行一些更改即可。

相關問題