Jonny P
Jonny P

Reputation: 453

How can i extract a line from a PHP array that returns multiple strings

I have a PHP variable named $user, i set the variable like this:

$user = $e->getUser();

When i echo $user i receive the following data in an array:

Array
(
    [user_id] => 1
    [login] => test
    [pass] => ***
    [remember_key] => 
    [pass_dattm] => 2019-10-11 19:35:08
    [email] => [email protected]
    [name_f] => test
    [name_l] => test
    [street] => 
    [street2] => 
    [city] => 
    [state] => 
    [zip] => 
)

I want to echo just the value of 'login' to a file using exec, however i have tried different solutions found on stackoverflow but it does not seem to work for me. I have tried:

$user = $e->getUser();
exec("echo '".$user['login']."' >> /tmp/test");

and also tried :

$user = $e->getUser()->login();
exec("echo '".$user."' >> /tmp/test");

However both return a blank variable. How can i achieve this?

Upvotes: 0

Views: 42

Answers (1)

Barmar
Barmar

Reputation: 780929

You should use escapeshellarg() to ensure that it's properly escaped.

$user = $e->getUser();
$login = escapeshellarg($user['login']);
exec("echo $login >> /tmp/test");

But there's little reason to use exec() for this, since PHP can write to files itself.

file_put_contents("/tmp/test", $user['login'] . "\n", FILE_APPEND);

Upvotes: 3

Related Questions