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

PHP代码调整:实现preg_match未匹配或匹配空div时返回N/A

Fix for Handling Empty or Missing Divs in Your PHP Function

Got it, let's sort this out for you! The problem with your current code is that when the target div is empty (like <div class="classname"></div>) or only contains whitespace, your regex still captures an empty/whitespace string—so preg_match returns true, and you end up returning that empty value instead of 'N/A'. We need to adjust the logic to cover both scenarios: no matching div found, or a matching div with empty/whitespace-only content.

Solution 1: Check Trimmed Content (Most Flexible)

This approach keeps your existing regex but adds a check to ensure the captured content isn't just empty or whitespace. It's easy to read and handles edge cases like divs with spaces, tabs, or newlines:

function getaname($name) {
    $result = file_get_contents('https://www.something.com');
    $regex = '/<div class="classname">([^<]*)<\/div>/';
    if (preg_match($regex, $result, $matches)) {
        // Trim whitespace to handle cases like <div class="classname">   </div>
        $content = trim($matches[1]);
        // Return content only if it's not empty after trimming
        return !empty($content) ? $content : 'N/A';
    } else {
        return 'N/A';
    }
}

How It Works:

  • We still use your original regex to capture content inside the div.
  • trim($matches[1]) removes any leading/trailing whitespace (spaces, tabs, newlines) from the captured content.
  • !empty($content) checks if the trimmed content has actual text. If yes, return it; if not, return 'N/A'.
  • If no div is found at all, preg_match returns false, and we fall back to returning 'N/A' as before.

Solution 2: Adjust the Regex (Match Only Non-Empty Content)

If you prefer to handle this directly in the regex, you can modify it to only match divs with non-whitespace content. This way, empty/whitespace divs won't trigger a match at all:

function getaname($name) {
    $result = file_get_contents('https://www.something.com');
    // Regex matches divs with at least one non-whitespace character inside
    $regex = '/<div class="classname">\s*([^<\s].*?)\s*<\/div>/s';
    if (preg_match($regex, $result, $matches)) {
        return $matches[1];
    } else {
        return 'N/A';
    }
}

How This Regex Works:

  • \s* matches any leading/trailing whitespace inside the div (so spaces around content are ignored).
  • [^<\s].*? ensures we capture content that starts with a non-whitespace, non-< character (to avoid matching empty content).
  • The /s modifier allows the . to match newlines, in case the div content spans multiple lines.

Recommendation

I'd go with Solution 1 because it's more straightforward and easier to maintain. It explicitly handles whitespace cases that might slip through regex adjustments, and it’s clearer for other developers reading your code to understand what’s happening.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:07:53