2015-02-09 56 views
2

我需要去除由joomla/K2創建的元描述中的一些花括號。Joomla:K2 - 如何使用preg_replace去除元描述中的花括號

我發現了兩個php解決方案,以剝離不需要的大括號:

$description = preg_replace('/{.+?}/', '', $description); 

$metaDescItem = str_replace('/{.+?}/', '', $metaDescItem); 

有其控制我的應用程序的內容不同的大括號:

{123456789}, {123456789,123456789}, {URL}, {}

最好的解決方案n會去掉元描述輸出中的任何大括號。

我是新來的PHP,我不知道哪個功能是正確的。

接下來的問題是,我不知道在哪裏插入功能K2的php文件。

我想我找到了合適的php文件,它生成了元描述。

下面是引自/components/com_k2/views/item/view.html.php

// Set metadata 
    if ($item->metadesc) 
    { 
     $document->setDescription((K2_JVERSION == '15') ? htmlspecialchars($item->metadesc, ENT_QUOTES, 'UTF-8') : $item->metadesc); 
    } 
    else 
    { 
     $metaDescItem = preg_replace("#{(.*?)}(.*?){/(.*?)}#s", '', $item->introtext.' '.$item->fulltext); 
     $metaDescItem = strip_tags($metaDescItem); 
     $metaDescItem = K2HelperUtilities::characterLimit($metaDescItem, $params->get('metaDescLimit', 150)); 
     $document->setDescription(K2_JVERSION == '15' ? $metaDescItem : html_entity_decode($metaDescItem)); 
    } 

回答

0

使用$description = preg_replace('@\{.+?\}@', '', $description); - 你需要{}前使用\因爲這些都是在正則表達式的特殊字符,所以你需要用反斜槓來轉義它們。

+0

謝謝!它的工作原理,但只剝去大括號。我們也想去掉大括號的內部。有什麼建議麼? Thx – Christoph 2015-02-09 20:05:39

+0

啊,我沒有得到它,我會改變我的答案。 – 2015-02-09 20:18:56

+0

非常感謝您的支持。有用!!! – Christoph 2015-02-09 20:50:54