Jump to content

Str_Replace Modules Problem


ds111

Recommended Posts

Hey everyone :D

 

  Ok so I have a little problem. I am allowing my users on the site to edit the sidebar and have variables like:

 

  {module.loginform}

  {module.username}

  {module.sitename}

 

etc, etc. In the SQL Database, the code is there for each of them. When they submit the sidebar to the server, it saves it. It usually ends up being something like:

 

Welcome {module.username} to {module.sitename}!

 

I've made a little script that searchs in the database then takes the {modules} and replaces the code for them in the sidebar.

So in the end it SHOULD look like:

 

Welcome <?php echo $_SESSION['username']; ?> to <?php echo $_SESSION['sitename']; ?>!

 

Which outputs:

 

Welcome, Bob to PHP Freaks!

 

But, instead, it outputs as:

 

Bob Welcome to!

 

Here's all the code related to sidebar:

 

//CODE THAT DOES REPLACING:
<?php
class modules {
function findModules($text) {

	require_once('registry/loader.php');
	$loader = new loader;
	$loader->load("connect_db", "function");

	connect_db();
	$query = "SELECT * from modules";
	$run_query = mysql_query($query);
	$num_modules = mysql_num_rows($run_query); 			// Get the number of modules
	$i = 0;				// The index of how many modules we've replaced so far

	while($i != $num_modules) {
		$module_name = mysql_result($run_query, $i, "modname");		// ALWAYS in format: {module.XXXX}  Where XXXX is the module name
		$module_html = mysql_result($run_query, $i, "modhtml");

		$module_html = str_replace('<?php', '',$module_html); // Strip Open PHP Tags
		$module_html = str_replace('<?', '',$module_html); // Strip Open PHP Tags (Other type)

		$module_html = str_replace('?>', '',$module_html); // Strip Closed PHP Tags
		$text = str_replace($module_name, $module_html, $text, $count);
		require_once('registry/functions/function.connect_db.php');

		$count = (int)$count;
		if($count != 0) {
			$text = str_replace($module_html, eval($module_html), $text);

			?>
			<script>
			alert("<?php echo $text; ?>"); //For debug purposes
			</script>
			<?php
		}

		$i++; // Increase amount of modules we've replaced
	}
	/* Done with replacing ALL modules recognized :: return the text*/
	return $text; 
}

}
?>

 

Code that calls it:

require_once('registry/functions/function.connect_db.php');
		connect_db();
		$query = "SELECT * from sidebar WHERE type='guest'";
		$run_query = mysql_query($query);
		$sidebar = mysql_result($run_query, 0, "html");
		require_once('registry/classes/class.modules.php');
		$modules = new modules;
		require_once('templates/'.$_SESSION['skin'].'/sidebar.php');

 

and in sidebar.php it just has <?php echo $sidebar; ?> embedded in some html code.

 

 

I hope I've explained it enough. Can anyone help?

 

Link to comment
https://forums.phpfreaks.com/topic/138642-str_replace-modules-problem/
Share on other sites

try this:

//CODE THAT DOES REPLACING:
<?php
class modules {
   function findModules($text) {

      require_once('registry/loader.php');
      $loader = new loader;
      $loader->load("connect_db", "function");
      
      connect_db();
      $query = "SELECT * from modules";
      $run_query = mysql_query($query);
      $num_modules = mysql_num_rows($run_query);          // Get the number of modules
      $i = 0;            // The index of how many modules we've replaced so far
      
      while($i != $num_modules) {
         $module_name = mysql_result($run_query, $i, "modname");      // ALWAYS in format: {module.XXXX}  Where XXXX is the module name
         $module_html = mysql_result($run_query, $i, "modhtml");
         
         $module_html = str_replace('<?php', '',$module_html); // Strip Open PHP Tags
         $module_html = str_replace('<?', '',$module_html); // Strip Open PHP Tags (Other type)
         
         $module_html = str_replace('?>', '',$module_html); // Strip Closed PHP Tags
         $text = str_replace($module_name, $module_html, $text, $count);
         require_once('registry/functions/function.connect_db.php');
      
         $count = (int)$count;
         if($count != 0) {
            $text = str_replace($module_html, eval($module_html), $text);
               
            ?>
            <script>
            alert("<?php echo $text; ?>"); //For debug purposes
            </script>
            <?php
         }

         $i++; // Increase amount of modules we've replaced
      }
      /* Done with replacing ALL modules recognized :: return the text*/
      return $text; 
   }
   
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

you missed a single quote on line 23 :D >.< im so glad i spotted that lol

 

best of luck!

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.