Jump to content


Photo

str_replace problems


  • Please log in to reply
2 replies to this topic

#1 z3rb

z3rb
  • Members
  • Pip
  • Newbie
  • 2 posts

Posted 03 June 2006 - 03:08 PM

function.php
//Get the HTML_DOCTYPE from gui/html_doctype.txt
   function html_doctype(){
      if(file_exists("gui/html_doctype.txt")){
         $style['html_doctype'] = file_get_contents("gui/html_doctype.txt");
         echo $style['html_doctype'];
      }else{
         echo("Fatal Error: HTML_DOCTYPE not found. Please re-upload html_doctype.txt");
      }
   }

//Change {config|X} to $config['X']
   function parse_config($string){
      $old = array("\{config|url\}", "\{config|mysql_server\}", "\{config|mysql_db\}");
      $new = array($config['url'], $config['sqlserver'], $config['dbname']);
      $new_string = str_replace($old, $new, $string);
      $template_parsed['config'] = $new_string;
   }

//Generate Header
   $template_unparsed['header'] = html_doctype();
   parse_config($template_unparsed['header']);
   echo $template_parsed['config'];

html_doctype.txt
<!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" xml:lang="en" lang="en-AU">

<head>
<title>Perth Academy</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="{config|url}/gui/css/style.css" />
</head>

It doesn't want to replace {config|url} with $config[url]...

Any ideas?

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 June 2006 - 03:20 PM

In this piece of code:
<?php
//Change {config|X} to $config['X']
   function parse_config($string){
      $old = array("\{config|url\}", "\{config|mysql_server\}", "\{config|mysql_db\}");
      $new = array($config['url'], $config['sqlserver'], $config['dbname']);
      $new_string = str_replace($old, $new, $string);
      $template_parsed['config'] = $new_string;
   }
?>
Where is the $config array initialized? I don't see it anywhere, so your $new array doesn't contain what you think it does.

I would pass the $old & $new arrays to your function instead of defining them in the function.

<?php
   $old = array('{config|url}', '{config|mysql_server}', '{config|mysql_db}');
   $new = array($config['url'], $config['sqlserver'], $config['dbname']);
   $template_unparsed['header'] = html_doctype();
   parse_config($template_unparsed['header']);
   echo $template_parsed['config'];

   function parse_config($string,$o, $n){
      $new_string = str_replace($o, $n, $string);
      $template_parsed['config'] = $new_string;
   }
?>

Ken

#3 z3rb

z3rb
  • Members
  • Pip
  • Newbie
  • 2 posts

Posted 03 June 2006 - 03:26 PM

[!--quoteo(post=379637:date=Jun 3 2006, 10:20 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 3 2006, 10:20 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
In this piece of code:
<?php
//Change {config|X} to $config['X']
   function parse_config($string){
      $old = array("\{config|url\}", "\{config|mysql_server\}", "\{config|mysql_db\}");
      $new = array($config['url'], $config['sqlserver'], $config['dbname']);
      $new_string = str_replace($old, $new, $string);
      $template_parsed['config'] = $new_string;
   }
?>
Where is the $config array initialized? I don't see it anywhere, so your $new array doesn't contain what you think it does.

I would pass the $old & $new arrays to your function instead of defining them in the function.

<?php
   $old = array('{config|url}', '{config|mysql_server}', '{config|mysql_db}');
   $new = array($config['url'], $config['sqlserver'], $config['dbname']);
   $template_unparsed['header'] = html_doctype();
   parse_config($template_unparsed['header']);
   echo $template_parsed['config'];

   function parse_config($string,$o, $n){
      $new_string = str_replace($o, $n, $string);
      $template_parsed['config'] = $new_string;
   }
?>

Ken
[/quote]
Thanks. The $config[x] variables are set in another php file called config.php (don't worry, that is required()).

config.php
//MySQL Details
   //MySQL Database Name
   $config['dbname'] = "";
   //MySQL Server
   $config['sqlserver'] = "localhost";
   //MySQL Username
   $config['sqlusername'] = "";
   //MySQL Password
   $config['sqlpassword'] = "";

//Server Details
   //URL of PASite setup
   $config['url'] = "http://localhost/perthacademy"; //DO NOT END IN TRAILING SLASH.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users