Sonny King
Sonny King

Reputation: 73

preg_replace in order string is found

I have a shortcode system that will fire a function if a short code is found on page load like so:

[[gallery]]

Problem is I need to print any text or other html found between the shortcodes in the order they are found.

[[gallery]]
This is a nice gallery

[[blog id=1]]
This is a recent blog

[[video]]
Here is a cool video!

what I have so far is this:

if no [[shortcodes]] are found, no need to run shortcode function, we just print the body of the content.

           if(!preg_match('#\[\[(.*?)\]\]#', $page_content, $m1)){
           print $page_content;
           }

this removes any shortcodes and prints the text, but only prints it above all the shortcodes that are found.

           if(preg_match('#\[\[(.*?)\]\]#', $page_content, $m1)){
           $theFunction1 = $m1[0];
           $page_text = preg_replace('#\[\[(.*?)\]\]#', '',$page_content);
           print $page_text;
           }

if we find any [[shortcodes]], we loop thru them and pass them to a function to handle them with a callback.

           if(preg_match_all('#\[\[(.*?)\]\]#', $page_content, $m)){
           foreach($m[0] as $theFunction){
           print shortcodify($theFunction);
           } 
           }

preg_replace does not display them in the order of the $page_content var as they are found. Even when I put the preg_replace in the foreach loop I get results like this:

  This is a nice gallery
  This is a recent blog
  This is a recent blog
  [[gallery]] (gallery loads)


  This is a nice gallery
  This is a recent blog
  This is a recent blog
  [[blog id=1]] (the blog displays)

  This is a nice gallery
  This is a recent blog
  This is a recent blog
  [[video]] (video plays)

So, as you can see.. it duplicates all the occurrences between the shortcodes. I need to print them in order.

Upvotes: 0

Views: 197

Answers (1)

Toto
Toto

Reputation: 91488

You're printing the whole $page_text before calling shortcodify for each shortcode.

I'd do something like:

$page_content = <<<EOD
[[gallery]]
This is a nice gallery

[[blog id=1]]
This is a recent blog

[[video]]
Here is a cool video!

EOD;

if(preg_match('#\[\[(.*?)\]\]#', $page_content)){ // there are shortcodes
    $items = explode("\n", $page_content);        // split on line break --> array of lines
    foreach($items as $item) {    // for each line
        if(preg_match('#\[\[(.*?)\]\]#', $item)){  // there is a shortcode in this line
            // replace shortcode by the resulting value
            $item = preg_replace_callback('#\[\[(.*?)\]\]#', 
                        function ($m) {
                             shortcodify($m[1]);
                        },
                        $item);
        }
        // print the current line
        print "$item\n";
    }
} else {    // there are no shortcodes
    print $page_content;
}

function shortcodify($theFunction) {
    print "running $theFunction";
}

Output:

running gallery
This is a nice gallery

running blog id=1
This is a recent blog

running video
Here is a cool video!

Upvotes: 1

Related Questions