Joomla 3 content plugn foreach $row->fulltext duplicates

This forum is for general questions about extensions for Joomla! 3.x.

Moderators: pe7er, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Locked
vbarlakoski
Joomla! Intern
Joomla! Intern
Posts: 64
Joined: Sun May 02, 2010 9:50 pm

Joomla 3 content plugn foreach $row->fulltext duplicates

Post by vbarlakoski » Mon Sep 07, 2015 12:25 pm

I am setting up a new content plugin for Joomla 3, that should replace plugin tags with html content. Everything works fine till the moment when i am preg_replace plugin tags in $row->fulltext.

Here is the plugin code

Code: Select all

public function onContentPrepare($context, &$row, &$params, $page = 0) {
$pattern = '#\{uni\}(.*){\/uni\}#sU';
    preg_match_all($pattern, $row->fulltext, $matches);

    foreach($matches[1] as $k=>$uni){

        preg_match('/\{uni-title\}(.*)[\{]/Ui', $uni, $unititle);
        preg_match('/\{uni-text\}(.*)/si', $uni, $unitext);

        $titleID = str_replace(' ', '_', trim($unititle[1]));

        $newString = '<span id="'.$titleID.'">'.$unititle[1].'</span><div class="university-info-holder"><div class="university-info"><i class="icon icon-close"></i>'.$unitext[1].'</div></div>';

        $row->fulltext = preg_replace($pattern,$newString,$row->fulltext);

    }
}
Any ideas, why it duplicates first found match, as many times as foreach goes?

Just to mention, if i do:

Code: Select all

echo $unititle[1];
inside the foreach, items are rendered as it should be.

Locked

Return to “Extensions for Joomla! 3.x”