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

如何在PHP大型数组中匹配指定字符串并获取对应键名?

Fixing PHP Array Search for Makemkvcon Disk Size

Hey there! Let's break down why your current code isn't grabbing the right line from your saved BD_Info file, and get you the disk size you need efficiently.

The Problem with Your Current Code

Your original array_search('TINFO:0,10,0,', $BD_Info_array) call has two key issues:

  1. Exact match requirement: array_search looks for elements that are exactly equal to your target string. But each line in your file is something like TINFO: 0,10,0, "36.9 GB" — which includes the target plus extra text (and a space after TINFO: that your search string is missing!).
  2. False handling: When array_search can't find an exact match, it returns false. PHP treats false as 0 when used as an array index, so you end up getting the first line of the file instead of the correct matching line.

The Fix: Search for Lines Containing the Target

Instead of looking for an exact match, we need to check each line to see if it contains your fixed TINFO: 0,10,0, identifier. Here are two reliable ways to do this:

Option 1: Foreach Loop (Simple & Explicit)

This loops through each line and stops as soon as it finds a match, which is efficient for large arrays:

<?php
// Load the file content into an array
$BD_Info_array = file("../BD_Info");
// Target string with the correct space (matches your sample output)
$target_identifier = 'TINFO: 0,10,0,';
$matched_line = null;

foreach ($BD_Info_array as $line) {
    // Trim whitespace/newlines and check if the line contains our target
    if (stripos(trim($line), $target_identifier) !== false) {
        $matched_line = trim($line);
        break; // Stop searching once we find the first match
    }
}

if ($matched_line) {
    // Extract the size (same logic as your original shell command)
    $size = substr($matched_line, strlen($target_identifier) + 1, -1);
    echo $size; // Outputs something like "36.9 GB" or "5.7 GB"
} else {
    echo "Disk size information not found in the file.";
}
?>

Option 2: Array Filter (Concise)

If you prefer a more functional approach, array_filter can filter out all non-matching lines:

<?php
$BD_Info_array = file("../BD_Info");
$target_identifier = 'TINFO: 0,10,0,';

// Filter lines to only those containing the target identifier
$matching_lines = array_filter($BD_Info_array, function($line) use ($target_identifier) {
    return stripos(trim($line), $target_identifier) !== false;
});

if (!empty($matching_lines)) {
    // Get the first matching line
    $matched_line = trim(reset($matching_lines));
    $size = substr($matched_line, strlen($target_identifier) + 1, -1);
    echo $size;
} else {
    echo "Disk size information not found in the file.";
}
?>

Key Notes

  • Case insensitivity: I used stripos instead of strpos to match the -i flag in your original grep command — this ensures it works even if the output has uppercase/lowercase variations.
  • Trim whitespace: trim() removes extra newlines or spaces from each line, which prevents false negatives from invisible characters.
  • Efficiency: Both methods are way faster than re-running makemkvcon every time, since you're reading from a local file instead of executing an external command.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:19:09