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?

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.