Jump to content

Str_Replace, Returning "array" When $Replace Is An Array


Dannymh

Recommended Posts

Hi,

 

I am writing some code as a forum addon for myBB to make a content delivery page. In doing so I need to add blocks in user defined areas.

 

I am doing this by having users create their block areas, then defining the area in the template with [ba]block_area_name[/ba]. I then go through and grab all of the blocks for that area from the database and use file_get_contents to grab them and display them using str_replace (If you can show me a better way through just preg_replace or eval I would be happy though :) )

 

Anyway my issue is that when you I do $page = str_replace('[ba]'.$match.'[/ba]', $block_include, $page); where block_include is an array, all I get back is teh text

 

"Array".

 

When I print_r($block_include); it returns the data from those files.

 

My code so far at proof of concept stage is

 

function cm_get_blocks(&$page)    {

   global $db, $config, $mybb;

   $pattern= "/\[ba\](.*?)\[\/ba\]/is";    
   //the variable for the replace
   preg_match_all($pattern, $page, $matches);

   foreach($matches[1] as $match)    {

       //We need to get the ID of the block area for later use. We could do this with a join, but the performance value isn't there as it should be an inexpensive query
       $query = $db->query("Select ID from ".TABLE_PREFIX."brookie_cms_general_data where type='blockarea' and title='".$match."'");
       $block_area_id = $db->fetch_field($query, "ID");


       if($block_area_id)    {
           //get all of the blocks for the block are, in order and replace them.
           $block_query = $db->query("select block_title, block_folder from ".TABLE_PREFIX."brookie_cms_blocks where area=".$block_area_id);

           while($result=$db->fetch_array($block_query))    {

               echo $result[block_title]."<br>";

               $block_include[] = file_get_contents("http://localhost:85/mybb/inc/plugins/brookie_cms/blocks/".$result["block_folder"]."/index.php");                
           }

       }    else    {
           $page = str_replace('[ba]'.$match.'[/ba]', "", $page);
       }
       $page = str_replace('[ba]'.$match.'[/ba]', $block_include, $page);
   }
   return $page;
}

 

If I move $page = str_replace('[ba]'.$match.'[/ba]', $block_include, $page); to just before the return then it doesn't do the replce. If I move it to inside the while loop and make $block_include not an array then it will obviously work for the first block but no others because we have replaced the block already.

 

Can anyone think why it won't work?

 

cheers

Dan

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.