Search code examples
phpjoomlametadatacurly-bracketsk2

Joomla: K2 - How to strip out curly brackets from meta description using preg_replace


I need to strip out some curly brackets from the meta description which is created by joomla/K2.

I found two php solutions to strip unneeded curly brackets:

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

and

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

There are different curly brackets which control the content of my app:

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

The best solution would be to get rid of any curly bracket in the meta description output.

I am new to php and I am not sure which function is right.

Next problem is, that I don't know where to insert the function in the php file of K2.

I think I found the right php file which generates the meta description.

Here is the quote from /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));
    }

Solution

  • Use $description = preg_replace( '@\{.+?\}@', '', $description); - you need to use the \ before { and } because these are special characters in regex, so you need to escape them with backslash.