如何在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:
- Exact match requirement:
array_searchlooks for elements that are exactly equal to your target string. But each line in your file is something likeTINFO: 0,10,0, "36.9 GB"— which includes the target plus extra text (and a space afterTINFO:that your search string is missing!). - False handling: When
array_searchcan't find an exact match, it returnsfalse. PHP treatsfalseas0when 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
striposinstead ofstrposto match the-iflag in your originalgrepcommand — 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
makemkvconevery time, since you're reading from a local file instead of executing an external command.
内容的提问来源于stack exchange,提问作者Sub S7v7n
相关产品推荐
相关产品推荐

