JakkkeM Posted December 30, 2012 Share Posted December 30, 2012 Hi, I'm using a preg_replace to change [menu] into Menu::makeMenu(); , but when it executes the replace, for some reason it's sending my code to the first line after the <body> tag rather than where the [menu] was in the first place. Preg Replace: function formatPage($transform){ $a = array( "/\[element=\"(.*?)\"\]/ise", "/\ /ise", "/\[menu]/ise", "/\[login-button]/ise", ); $b = array( "elementCreate(\"$1\");", "pageCreate();", "Menu::makeMenu();", "loginBtn();", ); $transform = preg_replace($a, $b, $transform); return $transform; } I can copy out the whole function and stuff for the Menu:: but I don't think it'll matter. Please help! Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/272515-rogue-preg-replace/ Share on other sites More sharing options...
gizmola Posted December 30, 2012 Share Posted December 30, 2012 There is no good reason to use a regex when you have an exact match like this. Use str_replace instead. Quote Link to comment https://forums.phpfreaks.com/topic/272515-rogue-preg-replace/#findComment-1402146 Share on other sites More sharing options...
JakkkeM Posted December 30, 2012 Author Share Posted December 30, 2012 Replaced that - no change :/ Quote Link to comment https://forums.phpfreaks.com/topic/272515-rogue-preg-replace/#findComment-1402149 Share on other sites More sharing options...
gizmola Posted December 30, 2012 Share Posted December 30, 2012 Really need to see code, and the string that you're trying to do this conversion on. Quote Link to comment https://forums.phpfreaks.com/topic/272515-rogue-preg-replace/#findComment-1402150 Share on other sites More sharing options...
JakkkeM Posted December 30, 2012 Author Share Posted December 30, 2012 The String: <div class="cfs_container"> <div class="container-fluid"> <div class="row-fluid"> <div class="span12"> <!-- Top Bar Content --> <div class="span4">[element="title"]</div> <div class="span8"> <div class="btn-toolbar" style="float:right;"> [menu] </div> </div> </div> </div> <div class="row-fluid"> <div class="span12">[element="scroll"]</div> </div> <div class="row-fluid"> <div class="span3" style="background-color:#000; padding:10px; color:#FFF;"> <!--Left content--> [element="left"] </div> <div class="span6" style="background-color:#000; padding:10px; color:#FFF;"> <!--Central content--> </div> <div class="span3" style="background-color:#000; padding:10px; color:#FFF;"> <!--Right content--> [element="right"] </div> </div> </div> <div class="container-fluid"> <span class="span12" style="color:#FFF; padding:10px;"> <center> [element="copyright"] </center> </span><!-- SPAN END --> </div><!-- CONTAINER FLUID END --> </div> <?php /* Functions to create dropdown menus dynamically for items in DB using twitter bootstrap markup */ class Menu { public static function makeMenu() { echo "<div class=\"btn-toolbar\" style=\"margin: 0; float:right;\">"; try{ $getAllMenus = DB::get()->query("SELECT menu_id FROM menus"); $result = $getAllMenus->fetchAll(); } catch (PDOException $e){ die("Query Error: ".$e->getMessage()); } foreach($result as $menu){ self::getMenu($menu['menu_id']); } echo "</div>"; } static function isDropdown($menu_id) { try{ $dropDownCheck = DB::get()->prepare("SELECT COUNT(*) FROM links WHERE assoc_menu = :assoc_menu"); $dropDownCheck->bindParam(':assoc_menu', $menu_id, PDO::PARAM_INT); $dropDownCheck->execute(); $result = $dropDownCheck->fetch(); } catch (PDOException $e){ die("Query Error: ".$e->getMessage()); } return ($result[0] > 0); } static function getMenuItems($menu_id) { // get Link data from the database try{ $linkData = DB::get()->prepare("SELECT * from links WHERE assoc_menu = :assoc_menu"); $linkData->bindParam(':assoc_menu', $menu_id, PDO::PARAM_INT); $linkData->execute(); $links = $linkData->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e){ die("Query Error: ".$e->getMessage()); } return($links); } public static function getMenu($menu_id) { // get Menu data from the database try{ $menuData = DB::get()->prepare("SELECT * from menus WHERE menu_id = :menu_id LIMIT 1"); $menuData->bindParam(':menu_id', $menu_id, PDO::PARAM_INT); $menuData->execute(); $menu = $menuData->fetch(PDO::FETCH_ASSOC); } catch (PDOException $e){ die("Query Error: ".$e->getMessage()); } if(!self::isDropdown($menu_id)){ // if the menu doesn't contain dropdown items $markup = "<div class=\"btn-group\"> <a class=\"btn ".$menu['btn_type']."\" href=\"#\">".$menu['title']."</a> </div>"; } else { // otherwise create dropdown items $links = self::getMenuItems($menu_id); $markup = "<div class=\"btn-group\"> <a class=\"btn ".$menu['btn_type']." dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">".$menu['title']." <span class=\"caret\"></span></a> <ul class=\"dropdown-menu\"> <!-- dropdown menu links -->"; // generate link for each dropdown item foreach($links as $link){ $markup.= "<li><a href=\"".$link['link_url']."\">".$link['link_title']."</a></li>"; } $markup.= "</ul></div>"; } echo $markup; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/272515-rogue-preg-replace/#findComment-1402151 Share on other sites More sharing options...
gizmola Posted December 30, 2012 Share Posted December 30, 2012 The string you provided has no body tag. Now that I'm clear on what you were doing, I'm going to hazard a guess that if you reorder your preg_replace so that you do the menu replacement first, you might not have the current issue you're facing. Quote Link to comment https://forums.phpfreaks.com/topic/272515-rogue-preg-replace/#findComment-1402154 Share on other sites More sharing options...
JakkkeM Posted December 30, 2012 Author Share Posted December 30, 2012 Had no need for a body tag - it was being echo'd onto a page with body tags already. Solved the problem. Had to make the function return rather than echo. Cheers for the string replace tip though! Quote Link to comment https://forums.phpfreaks.com/topic/272515-rogue-preg-replace/#findComment-1402168 Share on other sites More sharing options...
silkfire Posted December 30, 2012 Share Posted December 30, 2012 It's a good practice to let functions return something instead of echoing it out, because otherwise you lose the control of knowing where in the code execution the string will be echoed out. Quote Link to comment https://forums.phpfreaks.com/topic/272515-rogue-preg-replace/#findComment-1402182 Share on other sites More sharing options...
PFMaBiSmAd Posted December 30, 2012 Share Posted December 30, 2012 You have a previous thread concerning output being echoed in functions. Did you read that thread - http://forums.phpfreaks.com/topic/272367-function-echo-vs-return/ Quote Link to comment https://forums.phpfreaks.com/topic/272515-rogue-preg-replace/#findComment-1402188 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.