You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中如何在If语句中跳过空InnerText并处理延期比赛

解决延期比赛数据提取失效问题

问题详情

我原本想在innertext为空时跳过数据处理,但结果却出现了默认值。相关判断代码如下:

if (strip_tags($result[$c]->innertext) == '') {
    $c++;
    continue;
}

实际输出不符合预期,异常显示了无效内容。

EDIT2:执行var_dump($result[$c]->innertext)后发现,目标内容并非空值,而是包含了其他无效内容。

EDIT3:以下是完整代码,我用它提取日本J3联赛的球队名称和比赛结果,但遇到延期比赛(标记为POSTP.)时,跳过逻辑完全失效:

<?php
include('../simple_html_dom.php');

function getHTML($url,$timeout)
{
       $ch = curl_init($url); // 初始化curl请求
       curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // 设置请求头的UserAgent
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将响应写入变量而非直接输出
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 自动跟随重定向
       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // 设置连接超时时间
       curl_setopt($ch, CURLOPT_FAILONERROR, 1); // 遇到错误时终止请求
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
       return @curl_exec($ch);
}

$response=getHTML("https://www.betexplorer.com/soccer/japan/j3-league/results/",10);
$html = str_get_html($response);

$titles = $html->find("a[class=in-match]"); // 提取比赛名称
$result = $html->find("td[class=h-text-center]/a"); // 提取比赛结果/状态

$c=0; $b=0; $o=0; $z=0; $h=0; // 初始化计数器

foreach ($titles as $match) {   // 遍历所有比赛数据

    $match_status = $result[$h++];

    if (strip_tags($result[$c]->innertext) == 'POSTP.') {  // 尝试跳过延期比赛,但无效
        $c++;
        continue;
    }

 list($num1, $num2) = explode(':', $result[$c++]->innertext); // 分割比分
    $num1 = intval($num1);
    $num2 = intval($num2);
    $num3 = ($num1 + $num2);
    $risultato = ($num1 . '-' . $num2);

    list($home, $away) = explode(' - ', $titles[$z++]->innertext); // 分割主客队

$home = strip_tags($home);
$away = strip_tags($away);
$matchunit = $home . ' - ' . $away;

                            echo "<tr><td class='rtitle'>".
                            "<td> ".$matchunit.  "</td> / " .  // 输出比赛名称
                            "<td class='first-cell'>" . $risultato . "</td> " .
                            "</td></tr><br/>";
        
        }   // 结束foreach循环     
?>

修复方案

问题根源在于多计数器不同步,以及判断逻辑的位置和处理方式错误,具体修复如下:

1. 简化计数器,避免索引错位

删除无用的$b、$o、$h计数器,只用一个$c同步$titles和$result的索引,确保每场比赛的名称和结果/状态一一对应。

2. 优化延期比赛的判断逻辑

先提取结果的纯文本并去除空格,再判断是否为POSTP.,避免空格或HTML标签干扰判断。

3. 修正HTML输出的标签错误

原代码中<td class='rtitle'>未闭合就直接输出下一个<td>,导致HTML结构异常,需修正。

修改后的完整核心代码:

<?php
include('../simple_html_dom.php');

function getHTML($url,$timeout)
{
       $ch = curl_init($url);
       curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
       curl_setopt($ch, CURLOPT_FAILONERROR, 1);
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
       return @curl_exec($ch);
}

$response=getHTML("https://www.betexplorer.com/soccer/japan/j3-league/results/",10);
$html = str_get_html($response);

$titles = $html->find("a[class=in-match]");
$result = $html->find("td[class=h-text-center]/a");

$c = 0; // 仅保留一个同步计数器
foreach ($titles as $match) {
    // 获取结果的纯文本并去除首尾空格
    $result_text = trim(strip_tags($result[$c]->innertext));
    
    // 判断是否为延期比赛,是则跳过当前循环
    if ($result_text === 'POSTP.') {
        $c++;
        continue;
    }
    
    // 处理正常比赛的比分
    list($num1, $num2) = explode(':', $result_text);
    $num1 = intval($num1);
    $num2 = intval($num2);
    $risultato = $num1 . '-' . $num2;
    
    // 处理主客队名称
    list($home, $away) = explode(' - ', $match->innertext);
    $home = trim(strip_tags($home));
    $away = trim(strip_tags($away));
    $matchunit = $home . ' - ' . $away;
    
    // 修正后的HTML输出
    echo "<tr>
            <td class='rtitle'></td>
            <td>{$matchunit}</td>
            <td>/</td>
            <td class='first-cell'>{$risultato}</td>
          </tr><br/>";
    
    $c++;
}
?>

额外说明

  • 使用trim()可以避免文本中的空格导致判断失效(比如POSTP.和POSTP.会被视为不同内容)。
  • 移除无用变量后,代码逻辑更清晰,减少了索引错位的可能性。

内容的提问来源于stack exchange,提问作者Marci

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 21:45:34