Search code examples
phpstringexplodestring-matching

catch notification of explode()


I process input of users.

Input should have this format:

text. address: xyz

Now I tried to use:

$address = explode('address: ', $text)[1];

But this causes an error in case of user input doesn't match the needed format.

How do I prevent this (maybe including a check), so script won't trigger a notice?


Solution

  • First assign variable:

    $address = explode("address: ", $text);
    

    Then check if is set key 1:

    if (isset($address[1]) {
        $address = $address[1];
    }
    

    Then you shouldn't have any notices because next time you'll to existing parameter in array.