PHP代码出现Undefined offset错误,请求排查解决方法
Hey there, let’s break down what’s causing that "Undefined offset" error and fix it up step by step.
What’s Triggering the Error?
The core issue is your loop runs a hardcoded 50 times, but the $sport and $channel arrays generated by your ara() function almost certainly don’t have 50 elements each. When $i exceeds the last valid index of either array (say, if there are only 12 matches total), trying to access $sport[$i] or $channel[$i] throws the error because that index doesn’t exist.
Also, using the @ error suppressor in your ara() function hides useful debugging clues (like if preg_match_all fails silently) and makes it harder to catch other underlying issues.
Step-by-Step Fixes
1. Loop Only Through Existing Elements
Instead of forcing 50 iterations, loop only as many times as there are matching elements in both arrays. Use min(count($sport), count($channel)) to get the shorter array length—this ensures you never try to access an element that exists in one array but not the other:
$maxItems = min(count($sport), count($channel)); for ($i = 0; $i < $maxItems; $i++) { echo "<span class='linko'><a target='_blank' href='example.org/live1.php?id=" . $channel[$i] . "'>$sport[$i]</a>...</span>"; }
2. Improve the ara() Function
Remove the error suppressor and ensure the function always returns an array (even if no matches are found) using PHP’s null coalescing operator (??):
function ara($one, $two, $three) { $matches = []; // Only run the regex if input content isn't empty if (!empty($three)) { preg_match_all('/' . preg_quote($one, '/') . '(.*?)' . preg_quote($two, '/') . '/i', $three, $matches); } // Return matched group, or empty array if no results return $matches[1] ?? []; }
3. Add Error Handling for file_get_contents
Your $link is missing the HTTP/HTTPS protocol, which will cause file_get_contents to fail. Add the protocol and check if the content was fetched successfully:
$link = "http://example.com"; // Add http:// or https:// $article = file_get_contents($link); if ($article === false) { die("Failed to load the webpage. Check the URL or network connection."); }
Full Fixed Code
Here’s the complete code with all fixes (plus an XSS prevention tweak):
function ara($one, $two, $three) { $matches = []; if (!empty($three)) { preg_match_all('/' . preg_quote($one, '/') . '(.*?)' . preg_quote($two, '/') . '/i', $three, $matches); } return $matches[1] ?? []; } $link = "http://example.com"; $article = file_get_contents($link); if ($article === false) { die("Failed to fetch the webpage."); } $sport = ara('data-bin="','"',$article); $channel = ara('data-videobin="','"',$article); $maxItems = min(count($sport), count($channel)); for ($i = 0; $i < $maxItems; $i++) { // Use htmlspecialchars to prevent XSS vulnerabilities echo "<span class='linko'><a target='_blank' href='example.org/live1.php?id=" . htmlspecialchars($channel[$i]) . "'>" . htmlspecialchars($sport[$i]) . "</a>...</span>"; }
内容的提问来源于stack exchange,提问作者Ali Keskin

