我使用我的WordPress下面的代碼安裝:preg_replace_callback錯誤
function add_glossary_links($content) {
global $wpdb, $wordlist;
if (!$wordlist && !$wordlist = get_option('wordlist')) {
mysql_query('SET SESSION group_concat_max_len = 100000');
$wordlist = $wpdb->get_var('SELECT GROUP_CONCAT(DISTINCT post_title SEPARATOR "|") AS list FROM '.$wpdb->posts.' WHERE post_status="publish" AND post_type="glossary" AND post_parent>0');
add_option('wordlist', $wordlist);
}
$wordlist = str_replace(array(" ", "'", "."), array("\s", "\'", "\."), $wordlist);
echo $wordlist;
$content = preg_replace_callback(
'/\b('.$wordlist.')\b/i',
create_function(
'$matches',
'return "<a href=\"/glossary/" . strtolower(substr($matches[0],0,1) . "/" . $matches[0]) . "/\">" . $matches[0] . "</a>";'
),
$content
);
return preg_replace('/(<[^<]+)<a\s.*?>(.*?)<\/a>/si','$1$2', $content);
}
add_filter('the_content', 'add_glossary_links');
的想法是,我從我的數據庫中的單詞的列表;如果它們存在,我將它們替換爲適當術語表的鏈接。
$wordlist
的呼應,因爲這種:http://pastebin.com/6XnWBJwM
是我收到的錯誤是這樣的:
Warning: preg_replace_callback(): Unknown modifier 'c' in /my.website/wp-content/themes/mytheme/functions.php on line 384
384線是該段的最後一行:
$content = preg_replace_callback(
'/\b('.$wordlist.')\b/i',
create_function(
'$matches',
'return "<a href=\"/glossary/" . strtolower(substr($matches[0],0,1) . "/" . $matches[0]) . "/\">" . $matches[0] . "</a>";'
),
$content
);
我認爲正則表達式的格式和wordlist顯示的方式存在問題,但是我不能爲了我的生活而揣測它。
由於提前,
請注意,沒有第二個參數的'preg_quote()'不會轉義'/'。 – alex 2012-02-15 22:49:15
在編輯中修復。謝謝。 – 2012-02-15 22:51:28
我正在使用GROUP_CONCAT,因爲它是爲了速度目的而向我建議的 - 該列表源自數據庫中的1,750行。有沒有其他解決方法,而不改變SQL? – dunc 2012-02-16 00:43:04