Get the data inside a json_decode result

Asked

Viewed 93 times

1

How would I get the value of the login string and password inside a json_decode? What happens is that when I bring the result of a Curl:

$obj = json_decode($output);

The following message appears:

Your login details are: xxxxx login and password: YYYY

I tried to get the login as follows:

list($mensagem,$login) = explode("login:",$obj->info->cidade[0]->dados[0]->mensagem);

But he returns:

xxxxx and password: YYYY

You would need to take only the XXXXX for the login and YYYY for the password.

1 answer

4


It is possible to solve this problem with a regex : \w+ that capture : followed by a space and one or more characters(a-z0-9_).

To access the login and password use: $m[0][1] and $m[0][2], $m[0][0] can be discarded as it is the string login.

$str = 'Seus dados de acesso é: login: xxxxx e senha: YYYY';

preg_match_all('/: \w+/i', $str, $m);
echo "<pre>";
print_r($m);

Exit:

Array
(
    [0] => Array
        (
            [0] => : login
            [1] => : xxxxx
            [2] => : YYYY
        )

)
  • Perfect rray. Just one more question. The login and password can sometimes also contain numbers. Ex.: login: 2016_y1e3 and password: 1a3r. What would regex look like? Sorry for the question, because I don’t know much regex.

  • @Fox.11 edited the answer, see if this is it.

  • It worked!! Thanks for the help rray.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.