如何修复preg_replace警告?

时间:2013-10-04 20:09:14

标签: php preg-replace

PHP更新后,5.4.19面临一个之前不存在的新警告。它说:Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in ... on line 645

有一种方法:

private function BBtoHTML($input_string)
{
    $search = array(
        '/\[b\](.*?)\[\/b\]/is',
        '/\[i\](.*?)\[\/i\]/is',
        '/\[u\](.*?)\[\/u\]/is',
        '/\[s\](.*?)\[\/s\]/is',
        '/\[quote\](.*?)\[\/quote\]/is',
        '/\[code\](.*?)\[\/code\]/is',
        '/\[url\=(.*?)\](.*?)\[\/url\]/is',
        '/\[(left|center|right)\](.*?)\[\/(left|center|right)\]/is',
        '/\[font\=(.*?)\](.*?)\[\/font\]/is',
        '/\[size\=(.*?)\](.*?)\[\/size\]/is',
        '/\[color\=(.*?)\](.*?)\[\/color\]/is',
        '\{PAGEBREAK\}',
    ); 

    $replace = array(
        '/<strong>$1</strong>/',
        '/<em>$1</em>/',
        '/<span style="text-decoration: underline;">$1</span>/',
        '/<del>$1</del>/',
        '/<blockquote>$1</blockquote>/',
        '/<code>$1</code>/',
        '/<a href="$1" target="_blank">$2</a>/',
        '/<div style="text-align: $1;">$2</div>/',
        '/<span style="font-family: $1;">$2</span>/',
        '/<span style="font-size: $1;">$2</span>/',
        '/<span style="color: $1;">$2</span>/',
        '/<!--nextpage-->/'
    );

    return preg_replace($search, $replace, $input_string);
}

正如您可能理解的那样,645是return preg_replace($search, $replace, $input_string)

1 个答案:

答案 0 :(得分:1)

您拥有的最后一个模式是'\{PAGEBREAK\}'。如果您的意思是将文字\{PAGEBREAK\}与包含的反斜杠匹配,则模式应为:

     '/\\\\{PAGEBREAK\\\\}/',

如果您要匹配{PAGEBREAK},则模式应为:

     '/{PAGEBREAK}/',