1

Running the following code:

        preg_match("/^top\-sites\-csv\_(.+)\_/i", $file, $matches);
    $site_id = $matches[1];

And it keeps generating this notice:

PHP Notice:  Undefined offset

I guess its happening when the regex does not find a match. problem is that the script is on a cron and my error log grows huge in no time and then needs manual cleaning..

3
  • its like preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3); Commented Nov 5, 2012 at 8:28
  • Add an if block to see if the array key exists, and if not, substitute something else for the site id? Commented Nov 5, 2012 at 8:28
  • You could do if (preg_match($pattern, $text, $matches)) to check if there were any matches. Commented Nov 5, 2012 at 8:31

2 Answers 2

2

just check $matches using isset() before working with ist:

if(isset($matches[1])){
  $site_id = $matches[1];
  // do more here
}
Sign up to request clarification or add additional context in comments.

Comments

0

If preg_match did not find a match, $matches is an empty array. So you should check if preg_match found an match before accessing $matches[1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.