Jump to content

I am tired, so I might be missing somthing. What is there a space added?


Lamez

Recommended Posts

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?

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 = "[email protected]"

robot_name = "Robot"

 

**Email Subjects**

subject_email_valid="Some Domain - Email Validation"

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");
?>

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?

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] => [email protected]
    [robot_name] => Robot
    [subject_email_valid] => Some Domain - Email Validation
)

 

Ken

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

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

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");
?>

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

 

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.