0

I have a Joomla plugin (not important in this context), which is designed to take an input with a load of numbers (within a paragraph of text) and replace them with a series of s.

My problem is that I need to do a preg_replace on my $article->text, but I don't know how to then apply the changes to the matched terms. I've seen the preg_replace_callback, but I don't know how I can call that within a function.

function onPrepareContent( &$article, &$params, $limitstart )
    {
        global $mainframe;
        // define the regular expression
        $pattern = "#{lotterynumbers}(.*?){/lotterynumbers}#s";
        if(isset($article->text)){
            preg_match($pattern, $article->text, $matches);
            $numbers = explode("," , $matches[1]);
            foreach ($numbers as $number) {
                echo "<div class='number'><span>" . $number . "</span></div>";  
            }
        }else{
            $article->text = 'No numbers';
        }
        return true;
    }

AMENDED CODE:

function onPrepareContent( &$article, &$params, $limitstart )
    {
        global $mainframe;
        // define the regular expression
        $pattern = "#{lotterynumbers}(.*?){/lotterynumbers}#s";
        if(isset($article->text)){
            preg_match($pattern, $article->text, $matches);
            $numbers = explode("," , $matches[1]);
            foreach ($numbers as $number) {
                $numberlist[] = "<div class='number'><span>" . $number . "</span></div>";   
            }
            $numberlist = implode("", $numberlist);
            $article->text = preg_replace($pattern, $numberlist, $article->text);

        }else{
            $article->text = 'No numbers';
        }
        return true;
    }
7
  • I've used that function before so i can help you but you must explain me better the question Commented May 13, 2010 at 15:36
  • Apologies guys, as I re-read it, I wondered if it made sense. I've since rewritten it to the desired effect, but it seems cumbersome. The desired outcome is: 1) Find the term {lotterynumbers} in the text 2) Extract the numbers (csv) and wrap a div around each one 3) Pump it back into $article->text I've placed my amended code in the question Commented May 13, 2010 at 15:41
  • Why the amended function doesn't work? It seems ok. Commented May 13, 2010 at 15:45
  • Just seems a bit cumbersome doing the preg_match, and then the preg_replace. Commented May 13, 2010 at 15:47
  • With the preg_replace_callback function you must do the preg_replace_callback and then a preg_replace so i don't think that it's better:) Commented May 13, 2010 at 15:49

1 Answer 1

1
function onPrepareContent( &$article, &$params, $limitstart )
{
    global $mainframe;
    // define the regular expression
    $pattern = "#{lotterynumbers}(.*?){/lotterynumbers}#s";
    if(isset($article->text)){
        $article->text=preg_replace_callback($pattern,create_function('$match','$init="<div class=\'number\'><span>";$out="</span></div>"; return $init.implode($out.$init,explode(",",$match[1])).$out;'),$article->text);

    }else{
        $article->text = 'No numbers';
    }
    return true;
}

I've not tested it but it should work

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mck89. I've credited you with the answer, though both methods work. It's a case of maintainability/readability vs technical solution. Useful for reference in either case. Pete

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.