1

Based on the this tutorial https://docs.joomla.org/J3.x:Creating_a_content_plugin, I managed to write my first content plugin with this helper.php code:

$regex_cat = '/{reillorama:(.*?)}/i';
    
preg_match_all($regex_cat, $article->text, $matches);
if ($matches)
{
    $path = JPluginHelper::getLayoutPath('content', 'reillorama');
    foreach ($matches[1] as $catid)
    {
        $image = new images();
        $image ->categoryid = $catid;
        $allimages = $image ->getImages();

        ob_start();
        include $path;
        $html = ob_get_contents();
        ob_end_clean();

        $regex_cat = '/{reillorama:' . str_replace('.', '\.', str_replace('/', '\/', $catid)) . '}/is';

        $article->text = preg_replace($regex_cat, $html, $article->text);
    }
}

This code takes the catid from my plugin syntax {reillorama:"catid"} where catid is a number.

Now I need two more params like "navigation"(charset) and "title"(number). The final syntax code looks like {reillorama:5/dots/1}.

How can I extract those values from the input code and parse them to my default.php tmpl?

1
  • You should not be using str_replace() calls to escape characters in your $regex_cat pattern -- this what preg_quote() is for. Your question requirements are not clear to me, so I cannot offer a refined solution for your issue. This SO page seems like a worthwhile read: stackoverflow.com/q/17792817/2943403
    – mickmackusa
    Nov 30 '18 at 12:43
0

here is a very comprehensive solution

https://github.com/GHSVS-de/GHSVSThings/blob/master/relaterquatch/plugintag-mit-Optionen.php

it explodes all values

0

I don't quite have complete clarity about what is optional and what is required in your article placeholders, but this whole task can be contained within a single preg_replace_callback() call.

The pattern that I have build will require the first argument (catid) in the placeholder; the additional two arguments will be optional, and the second argument (charset) is omittable when only the third argument (number) is given.

The pattern requires the first argument to be an integer, the second argument must be letters only, and the third argument must be an integer. If your requirements differ from these rules, it will be trivial to adjust.

Code: (Demo with 5 different sample article texts)

$articles = [
    (object)['text' => 'Some text {reillorama:5/dots/1} some text {reillorama:12/foo}'],
    (object)['text' => 'Only text with no placeholders'],
    (object)['text' => 'Some text {reillorama:33} some text {reillorama:1/bar} some text'],
    (object)['text' => 'Some text {reillorama:5/word} some text {reillorama:2} some text {reillorama:555555/noSpacesInWord/7777777}'],
    (object)['text' => 'The pattern also allows the middle parameter to be omitted {reillorama:6/7}'],
];

$firstLookup = [5 => 'five', 12 => 'twelve', 1 => 'one', 2 => 'two'];
$secondLookup = ['foo' => 'fighters', 'bar' => 'none', 'word' => 'up', 'noSpacesInWord' => 'replacement'];
$thirdLookup = [1 => 'another one', 7 => 'seven'];

foreach ($articles as $article) {
    $article->text = preg_replace_callback(
        '~{reillorama:(\d+)(?:/([a-z]+))?(?:/(\d+))?}~i',
        function($m) use ($firstLookup, $secondLookup, $thirdLookup) {
            return '**'
                . ($firstLookup[$m[1]] ?? '[first not found]')
                . ($m[2] === null ? '' : (isset($secondLookup[$m[2]]) ? $secondLookup[$m[2]] : '[second not found]'))
                . ($m[3] === null ? '' : (isset($thirdLookup[$m[3]]) ? $thirdLookup[$m[3]] : '[third not found]'))
                . '**';
            //var_export($m);  if you want to see what is in the matches array
        },
        $article->text,
        -1,
        $count,
        PREG_UNMATCHED_AS_NULL
    );
    echo "{$article->text}\n---\n";
}

Output:

Some text **five[second not found]another one** some text **twelvefighters**
---
Only text with no placeholders
---
Some text **[first not found]** some text **onenone** some text
---
Some text **fiveup** some text **two** some text **[first not found]replacement[third not found]**
---
The pattern also allows the middle parameter to be omitted **[first not found]seven**
---

Of course, when you implement this snippet in your application, you won't need the foreach() and you can simply return preg_replace_callback(... instead of overwriting the $article-text variable.

If you need a hand with tailoring this solution to your actual needs, I can assist with that.

I suppose your custom function within preg_replace_callback() might look a little like this:

function($m) use ($path) {
    $image = new images();
    $image->categoryid = $m[1];
    $allimages = $image->getImages();

    // I don't have any clue what you'd like to do with $m[2] and $m[3].

    ob_start();
    include $path;
    return ob_get_clean();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.