Dannymh Posted November 1, 2012 Share Posted November 1, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/270146-str_replace-returning-array-when-replace-is-an-array/ Share on other sites More sharing options...
kicken Posted November 1, 2012 Share Posted November 1, 2012 Join your $block_include array together into a string using implode prior to the replace. Quote Link to comment https://forums.phpfreaks.com/topic/270146-str_replace-returning-array-when-replace-is-an-array/#findComment-1389214 Share on other sites More sharing options...
Dannymh Posted November 1, 2012 Author Share Posted November 1, 2012 ahhh such an easy solution! I ended up changing it from an array to $block_include .= file_get_contents which amounts to the same I guess. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/270146-str_replace-returning-array-when-replace-is-an-array/#findComment-1389216 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.