开发者

PHP undefined offset or undefined index issue unresolved [duplicate]

开发者 https://www.devze.com 2023-01-16 02:02 出处:网络
This question already has answers here: 开发者_Python百科 Closed 10 years ago. Possible Duplicate: Why am I getting “Undefined index” from my PHP?
This question already has answers here: 开发者_Python百科 Closed 10 years ago.

Possible Duplicate:

Why am I getting “Undefined index” from my PHP?

$pattern2 = "/([A-Za-z0-9\.\-\_\!\#\$\%\&\'\*\+\/\=\?\^\`\{\|\}]+)\@([A-Za-z0-9.-_]+)(\.[A-Za-z]{2,5})/";

foreach ($lines as $email) {
    preg_match($pattern2,$email,$goodies);
    $goodies[0]=filter_var($goodies[0], FILTER_SANITIZE_EMAIL);

    if(filter_var($goodies[0], FILTER_VALIDATE_EMAIL)){
        array_push($good,$goodies[0]);
    }
}

I am pasting a bunch of data from an old flatfile in a textarea in attempt to grab out the emails.

Everything works and it pulls out the emails fine. The pattern2 is the only one I tried that was lose enough to work for me.

The problem is I am getting an undefined offset in my error log. On the line where the first goodies[0] appears.

I changed all of the goodies[0] to goodies['0'] and I get an undefined index error.

I have tried for hours to fix this and I am at my wits end.

Thanks in advance for anyone who can fix this for me!


I still don;t understand. Should I do an if preg_match to avoid this?

Yes - doing that (or running isset() on the $goodies array) will be the only way to address the issue.

$pattern2 = "/([A-Za-z0-9\.\-\_\!\#\$\%\&\'\*\+\/\=\?\^\`\{\|\}]+)\@([A-Za-z0-9.-_]+)(\.[A-Za-z]{2,5})/";

foreach ($lines as $email) {
    if( preg_match($pattern2,$email,$goodies) ) {
        $goodies[0]=filter_var($goodies[0], FILTER_SANITIZE_EMAIL);
        if(filter_var($goodies[0], FILTER_VALIDATE_EMAIL)){
            array_push($good,$goodies[0]);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消