Jump to content

Rogue Preg Replace?


JakkkeM

Recommended Posts


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.

Link to comment
https://forums.phpfreaks.com/topic/272515-rogue-preg-replace/
Share on other sites


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;


}
}



?>

Link to comment
https://forums.phpfreaks.com/topic/272515-rogue-preg-replace/#findComment-1402151
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/272515-rogue-preg-replace/#findComment-1402154
Share on other sites

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.