Lamez Posted August 17, 2010 Share Posted August 17, 2010 My Code is suppose to parse a text file and return the value that is attached to the variable. However when I try calling two different variables, one after the next, it adds a space. Here is the code: <?php define("FILE", "config/review_setup.txt"); function fix($var){ $var = strtolower($var); return str_replace(" ", "_", $var); } function findValue($var, $file = FILE){ $var = fix($var); $val = NULL; $fh = fopen(FILE, "r"); while(!feof($fh)){ $string = fgetss($fh); $i = stripos($string, "="); $f_var = str_replace(" ", "", substr($string, 0, $i) ); if($f_var == $var){ $val = str_replace("\"", "", substr($string, $i+1, strlen($string)) ); $val = str_replace(" ", "", $val); break; } } if(is_null($val)) $val = "DNE"; fclose($fh); } echo findValue("host"); echo findValue("port"); ?> Output of above: smtp1.servage.net 25 if I just do host, I get this: smtp1.servage.net Any thoughts? Quote Link to comment Share on other sites More sharing options...
Vivid Lust Posted August 17, 2010 Share Posted August 17, 2010 Perhaps the value of the variables in your file that you're parsing have a space after them which is causing this. Jake Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 17, 2010 Author Share Posted August 17, 2010 No, I have already checked. Thanks Though. Quote Link to comment Share on other sites More sharing options...
Vivid Lust Posted August 17, 2010 Share Posted August 17, 2010 Can you share the text file? Jake Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 17, 2010 Author Share Posted August 17, 2010 I suppose, I am going to take out the sensitive data. **Email Server Settings** host = "smtp1.servage.net" port = "25" username = "REMOVED" password = "REMOVED" **Robot Email Settings** robot_email = "robot@somedomain.com" robot_name = "Robot" **Email Subjects** subject_email_valid="Some Domain - Email Validation" Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 17, 2010 Author Share Posted August 17, 2010 I noticed in my OP that I forgot some code. Just a line or two, here it is amended. <?php define("FILE", "config/review_setup.txt"); function fix($var){ $var = strtolower($var); return str_replace(" ", "_", $var); } function findValue($var, $file = FILE){ $var = fix($var); $val = NULL; $fh = fopen(FILE, "r"); while(!feof($fh)){ $string = fgetss($fh); $i = stripos($string, "="); $f_var = str_replace(" ", "", substr($string, 0, $i) ); if($f_var == $var){ $val = str_replace("\"", "", substr($string, $i+1, strlen($string)) ); $val = str_replace(" ", "", $val); break; } } if(is_null($val)) $val = "DNE"; fclose($fh); return $val; } echo findValue("host"); echo findValue("port"); ?> Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 17, 2010 Author Share Posted August 17, 2010 Okay, I have been playing around with it, this is very strange to me. This seems like a logical error, but I cannot locate it. Just a reminder, here is the code: <?php define("FILE", "config/review_setup.txt"); function fix($var){ $var = strtolower($var); return str_replace(" ", "_", $var); } function findValue($var, $file = FILE){ $var = fix($var); $val = NULL; $fh = fopen(FILE, "r"); while($string = fgets($fh)){ $i = stripos($string, "="); $f_var = str_replace(" ", "", substr($string, 0, $i) ); if($f_var == $var){ $val = substr($string, $i+1, strlen($string)-1); $val = str_replace('"', "", $val); break; } } fclose($fh); return $val; } echo findValue("port"); echo "?"; echo findValue("host"); echo findValue("robot name"); ?> Notice the echo statements. Now here is the output of those echo statements: 25 ? smtp1.servage.net Robot Why in the world is it doing this? Quote Link to comment Share on other sites More sharing options...
trq Posted August 17, 2010 Share Posted August 17, 2010 A little off topic but are you aware of parse_ini_file? It would be allot more efficient than your current methods. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted August 17, 2010 Share Posted August 17, 2010 What you're seeing are not spaces, but end-of-line characters because you're not trimming them when you read in each line. Replace this line: <?php $string = fgetss($fh); ?> with <?php $string = trim(fgetss($fh)); ?> BTW, you might want to look at the function parse_ini_file to do all the "hard work". Using parse_ini_file on your file yields: <?php <?php $x = parse_ini_file('config/review_setup.txt'); print_r($x); ?> Array ( [host] => smtp1.servage.net [port] => 25 [username] => REMOVED [password] => REMOVED [robot_email] => robot@somedomain.com [robot_name] => Robot [subject_email_valid] => Some Domain - Email Validation ) Ken Quote Link to comment Share on other sites More sharing options...
kickstart Posted August 17, 2010 Share Posted August 17, 2010 Hi Just about to write what Ken has written above. It is a line feed character that you are displaying. Incidentally this becomes more obvious if you do a view source on the page when it is returned. All the best Keith Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 17, 2010 Author Share Posted August 17, 2010 Thank you guys so much. Once I get this working well. I will go back and use that function. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted August 17, 2010 Share Posted August 17, 2010 You really should use the parse_ini_file() function now. With your code, you are reading the whole file in every time you call your function. This is very inefficient. Using parse_ini_file, the file is read once. Ken Quote Link to comment Share on other sites More sharing options...
Lamez Posted August 17, 2010 Author Share Posted August 17, 2010 Okay I revised my function, just in case someone else needs it, here is what I have: <?php define("FILE", "config/review_setup.txt"); function fix($var){ $var = strtolower($var); return str_replace(" ", "_", $var); } /*function findValue($var, $file = FILE){ $var = fix($var); $val = NULL; $fh = fopen(FILE, "r"); while($string = trim(fgets($fh))){ $i = stripos($string, "="); $f_var = str_replace(" ", "", substr($string, 0, $i) ); if($f_var == $var){ $val = substr($string, $i+1, strlen($string)-1); $val = str_replace('"', "", $val); break; } } fclose($fh); return trim($val); }*/ function findValue($var, $file = FILE){ $val = parse_ini_file($file); return $val[fix($var)]; } echo findValue("host"); echo findValue("port"); echo findValue("username"); ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted August 18, 2010 Share Posted August 18, 2010 That's still not good, since you're still reading/parsing the file every time you call the function findValue. Do something like this: <?php define("FILE", "config/review_setup.txt"); function fix($var){ $var = strtolower($var); return str_replace(" ", "_", $var); } function findValue($var, $val){ return $val[fix($var)]; } $vals = parse_ini_file(FILE); echo findValue("host",$vals); echo findValue("port",$vals); echo findValue("username",$vals); ?> Ken Quote Link to comment 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.