2015-02-09 84 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