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

求助:如何通过PHP Curl解析M3U文件为数组(支持大文件)

Hey there! Let's figure out how to parse your abc.m3u file into a clean PHP array. Based on the sample content you shared, here's a straightforward, reliable approach that handles the M3U structure perfectly:

Step 1: Break Down the M3U Structure

Your file uses the standard EXTM3U format:

  • Starts with the #EXTM3U header
  • Each stream entry has two parts:
    1. #EXTINF:-1,CHANNEL_NAME (defines the channel name)
    2. The stream URL (either .m3u8 or .ts in your example)

Sometimes the content might be all in one line or split across lines—our code will handle both cases.

Step 2: PHP Parsing Implementation

Here's a complete code snippet to read and parse the file:

// 1. Read the M3U file content
$m3uFilePath = 'abc.m3u';
$m3uContent = file_get_contents($m3uFilePath);

if (!$m3uContent) {
    die("Oops! Couldn't read the file. Check the path and file permissions.");
}

// 2. Clean up the header and split into channel blocks
// Remove the initial #EXTM3U tag and any surrounding whitespace
$m3uContent = preg_replace('/^#EXTM3U\s*/', '', $m3uContent);

// Split the content into individual channel blocks (filter out empty entries)
$channelBlocks = array_filter(explode('#EXTINF:', $m3uContent));

// 3. Parse each block into channel data
$channels = [];
foreach ($channelBlocks as $blockIndex => $block) {
    // Use regex to extract channel name and stream URL
    // Matches format: "-1,CH1 https://example.com/stream.m3u8"
    preg_match('/-1,(.*?)\s+(https?:\/\/.*)/', trim($block), $matches);

    if (count($matches) === 3) {
        $channels[] = [
            'name' => trim($matches[1]),
            'url'  => trim($matches[2])
        ];
    } else {
        // Log a warning if a block can't be parsed
        error_log("Skipping invalid channel block #".($blockIndex + 1).": ".trim($block));
    }
}

// Verify the result (remove this in production)
print_r($channels);

What This Code Does:

  • Reads the file: Uses file_get_contents to load the M3U content (this method handles unformatted, single-line content better than line-by-line reading).
  • Cleans the header: Removes the #EXTM3U tag so we can focus on stream data.
  • Splits into blocks: Breaks the content into chunks for each channel using #EXTINF: as the delimiter.
  • Extracts data: Uses a regular expression to pull out the channel name and URL from each block, even if the content is on one line or split across lines.

Step 3: Using the Array with VLC/FFmpeg

Once you have the $channels array, you can easily pass stream URLs to media tools. Here are quick examples:

For FFmpeg/ffplay:

// Play the first channel with ffplay
if (!empty($channels)) {
    $streamUrl = escapeshellarg($channels[0]['url']);
    // Execute ffplay (adjust parameters as needed for your use case)
    exec("ffplay $streamUrl");
}

For VLC:

// Open the first channel in VLC
if (!empty($channels)) {
    $streamUrl = escapeshellarg($channels[0]['url']);
    exec("vlc $streamUrl");
}

Notes:

  • If your M3U file has thousands of entries, this code will still perform well—array operations and regex are efficient enough for large files.
  • The regex handles both .m3u8 HLS streams and .ts direct streams, as shown in your sample.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:31:43