Jump to content

Hello,

 

Why am I getting a empty string?

Quote

string strtok ( string $str , string $token )

$str = "
APP_NAME=Test
APP_TEST=Value
";

$envKey = "APP_NAME";

$oldValue = strtok($str, "{$envKEy}=");

var_dump($oldValue);

##

string(2) " " 

if I do $str = "APP_NAME=Test"; it works, but why doesn't it work on a newline?

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/903983-php-strtok-issues/
Share on other sites

Link to post
Share on other sites

11 minutes ago, Joveice said:

Hello,

 

Why am I getting a empty string?


$str = "
APP_NAME=Test
APP_TEST=Value
";

$envKey = "APP_NAME";

$oldValue = strtok($str, "{$envKEy}=");

var_dump($oldValue);

##

string(2) " " 

if I do $str = "APP_NAME=Test"; it works, but why doesn't it work on a newline?

I gave up. Gonna use this

preg_match("/{$envKey}=(.*?)[\r\n|\r|\n]/", $str, $matches);

 

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/903983-php-strtok-issues/#findComment-11121445
Share on other sites

Link to post
Share on other sites

Why not use a different .env file, open the file and get a array key on every new line?

 

So you can do 

 

if(in_array('APP_ENV', $env)){

}

Throw that in a nice function and then make a function that does the check to the value you want.

Quote or mention me if not feel ignored 

Link to comment
https://linustechtips.com/topic/903983-php-strtok-issues/#findComment-11121561
Share on other sites

Link to post
Share on other sites

5 minutes ago, Cruorzy said:

Why not use a different .env file, open the file and get a array key on every new line?

 

So you can do 

 


if(in_array('APP_ENV', $env)){

}

Throw that in a nice function and then make a function that does the check to the value you want.

Hm, I like the idea.

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/903983-php-strtok-issues/#findComment-11121573
Share on other sites

Link to post
Share on other sites

1 hour ago, Cruorzy said:

Why not use a different .env file, open the file and get a array key on every new line?

 

So you can do 

 


if(in_array('APP_ENV', $env)){

}

Throw that in a nice function and then make a function that does the check to the value you want.

Something like this?

 

$str = 'APP_NAME=Test
APP_TEST=Data
APP_TEST2="Spaces ?"';

$array = explode("\n", $str);
$data = [];
foreach($array as $item) {
	$temp = explode("=", $item);
	$data[$temp[0]] = $temp[1];
}
echo $data['APP_TEST2'];

But then how would I go about writing it to the file? I still need to do some sort of find and replace

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/903983-php-strtok-issues/#findComment-11121672
Share on other sites

Link to post
Share on other sites

2 hours ago, Cruorzy said:

Why would you want to write to a file from PHP? most of the time you set the .env to your dev environment and then its done.

Whats the idea?

To be able to change config on the fly or in etc a installer > which is what this was for. The guy who wrote it had it so it overwrote everything in the .env file. I submitted a pull request with the ability to only change what was needed and leave everything else alone.

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/903983-php-strtok-issues/#findComment-11123045
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×