📖
특정 확장자로 끝나는 전체 URL 추출

페이지 정보

본문

예제[code]$input = <<<EOD
curl 'https://video.com/video/20230517/index.m3u8' \\
  -H 'accept: */*' \\
  ...
EOD;[/code]요청 스니펫에서 https://video.com/video/20230517/index.m3u8 URL만 PHP로 정확히 추출하려면, 정규식을 사용하여 전체 문자열에서 .m3u8로 끝나는 URL을 찾아내는 방식이 가장 효율적입니다.[code]<?php
$input = <<<EOD
curl 'https://video.com/video/20230517/index.m3u8' \\
  -H 'accept: */*' \\
  ...
EOD;

// .m3u8로 끝나는 전체 URL 추출
preg_match('/https?:\/\/[^\s'"]+\.m3u8/', $input, $matches);

if (!empty($matches)) {
    echo "추출된 M3U8 URL: " . $matches[0];
} else {
    echo "❌ M3U8 URL을 찾을 수 없습니다.";
}
?>[/code]
결과[code]추출된 M3U8 URL: https://video.com/video/20230517/index.m3u8[/code]이렇게 하면 curl 또는 JavaScript fetch 코드 전체에서 .m3u8로 끝나는 정확한 URL만 추출할 수 있습니다.

여러 개의 .m3u8 URL을 모두 배열로 추출하려면 preg_match_all()을 사용하면 됩니다. 아래는 완전한 PHP 예제입니다.[code]<?php
$input = <<<EOD
fetch("https://video53.hav36.com/.../index.m3u8", {...});
curl 'https://cdn.example.com/playlist/test123.m3u8' -H 'referer: https://olpian.com/';
<script>var url = "https://abc.xyz/sample/output.m3u8";</script>
EOD;

// 모든 .m3u8로 끝나는 URL 추출
preg_match_all('/https?:\/\/[^\s'"]+\.m3u8/', $input, $matches);

if (!empty($matches[0])) {
    echo "🔍 추출된 M3U8 목록:\n";
    foreach ($matches[0] as $index => $url) {
        echo ($index + 1) . ". " . $url . "\n";
    }
} else {
    echo "❌ M3U8 URL을 찾을 수 없습니다.";
}
?>[/code]
🧾 출력 예시[code]🔍 추출된 M3U8 목록:
1. https://video53.hav36.com/.../index.m3u8
2. https://cdn.example.com/playlist/test123.m3u8
3. https://abc.xyz/sample/output.m3u8[/code]
.m3u8?token=처럼 쿼리 파라미터가 붙은 URL도 처리되게 확장
.m3u8 뒤에 ?token=, ?v=123 등의 쿼리스트링이 붙은 URL도 포함하려면 정규식을 약간 확장해주면 됩니다.
✅ 쿼리 파라미터 포함한 M3U8 URL 추출 코드[code]<?php
$input = <<<EOD
fetch("https://cdn.abc.com/video/stream.m3u8?token=abcdef", {...});
curl 'https://media.example.com/play/index.m3u8?v=12345';
<script>let x = "https://xyz.net/live/channel.m3u8";</script>
EOD;

// 쿼리스트링 포함한 .m3u8 URL 추출
preg_match_all('/https?:\/\/[^\s'"]+\.m3u8(?:\?[^\s'"]*)?/', $input, $matches);

if (!empty($matches[0])) {
    echo "🔍 추출된 M3U8 목록:\n";
    foreach ($matches[0] as $index => $url) {
        echo ($index + 1) . ". " . $url . "\n";
    }
} else {
    echo "❌ M3U8 URL을 찾을 수 없습니다.";
}
?>[/code]
🧾 출력 결과 예시[code]🔍 추출된 M3U8 목록:
1. https://cdn.abc.com/video/stream.m3u8?token=abcdef
2. https://media.example.com/play/index.m3u8?v=12345
3. https://xyz.net/live/channel.m3u8[/code]
.m3u8, .mpd, .ts 등 스트리밍 관련 확장자들을 전부 추출
확장자 뒤에 ?query=... 같은 것도 포함
✅ M3U8, MPD, TS URL 전부 추출 (쿼리스트링 포함)[code]<?php
$input = <<<EOD
var m3u8 = "https://stream.site.com/video.m3u8?token=abc123";
let mpd = "https://dash.server.com/media/stream.mpd";
curl "https://cdn.xyz.net/segments/0001.ts?auth=token";
EOD;

// 확장자: m3u8, mpd, ts (+ 쿼리 포함) 추출
preg_match_all('/https?:\/\/[^\s'"]+\.(m3u8|mpd|ts)(\?[^\s'"]*)?/', $input, $matches);

if (!empty($matches[0])) {
    echo "🔍 추출된 스트리밍 관련 URL:\n";
    foreach ($matches[0] as $index => $url) {
        echo ($index + 1) . ". " . $url . "\n";
    }
} else {
    echo "❌ 관련 URL을 찾을 수 없습니다.";
}
?>[/code]
🧾 출력 예시[code]🔍 추출된 스트리밍 관련 URL:
1. https://stream.site.com/video.m3u8?token=abc123
2. https://dash.server.com/media/stream.mpd
3. https://cdn.xyz.net/segments/0001.ts?auth=token[/code]
✅ 확장자 추가: .m3u8, .mpd, .ts, .mp4, .webm, .mkv
✅ 특정 도메인 필터링: 예를 들어 video53.hav36.com 또는 원하는 도메인만 추출

✅ 도메인 필터 + 확장자 다중 지원 PHP 코드[code]<?php
$input = <<<EOD
여기에 여러 줄의 코드 또는 텍스트 붙여넣기
EOD;

// 허용할 도메인 목록 (여러 개 가능)
$allowedDomains = [
    'video53.hav36.com',
    'cdn.example.com',
    'stream.site.com'
];

// 지원할 확장자 목록
$extPattern = 'm3u8|mpd|ts|mp4|webm|mkv';

// 전체 URL 추출 (확장자 + 쿼리 포함)
preg_match_all("/https?:\/\/[^\s'\"]+\.($extPattern)(\?[^\s'\"]*)?/", $input, $matches);

// 도메인 필터링
$filteredUrls = array_filter($matches[0], function($url) use ($allowedDomains) {
    foreach ($allowedDomains as $domain) {
        if (strpos($url, $domain) !== false) {
            return true;
        }
    }
    return false;
});

// 출력
if (!empty($filteredUrls)) {
    echo "🔍 필터링된 스트리밍 관련 URL:\n";
    foreach (array_values($filteredUrls) as $index => $url) {
        echo ($index + 1) . ". " . $url . "\n";
    }
} else {
    echo "❌ 해당 도메인에 맞는 스트리밍 URL을 찾을 수 없습니다.";
}
?>[/code]
✅ 필터 설정하는 방법
도메인을 자유롭게 추가/삭제:[code]$allowedDomains = [
    'video53.hav36.com',
    'olpian.com',
    'media.myserver.net'
];[/code]
확장자 추가/삭제도 가능:[code]$extPattern = 'm3u8|mpd|ts|mp4|webm|mkv|flv';[/code]
✅ 다중 확장자 지원 .m3u8, .mpd, .ts, .mp4, .webm, .mkv, .flv
✅ 도메인 필터링 지정한 도메인에 포함된 URL만 추출
✅ 중복 제거 같은 URL이 여러 번 등장해도 1회만 출력
✅ 해상도 필터링 (선택적) URL에 720p, 1080p, 480p, 등 해상도 포함된 경우만 추출
✅ JSON 출력 옵션 PHP 배열이 아닌 JSON 형식으로도 출력 가능

🧠 풀기능 PHP 코드[code]<?php
$input = <<<EOD
https://video.com/video/20250517/1bfbde0529c893e16766731f9d42cfaf/index.m3u8
https://cdn.example.com/movie_1080p.mp4?token=abc
https://olpian.com/stream/video_720p.m3u8
https://random.com/vid/clip.ts
https://video53.hav36.com/video/backup.flv?auth=xyz
https://cdn.example.com/movie_720p.mp4
https://cdn.example.com/movie_720p.mp4  <!-- duplicate
EOD;

// ✅ 사용자 설정
$allowedDomains = ['video53.hav36.com', 'cdn.example.com', 'olpian.com'];
$allowedResolutions = ['720p', '1080p', '480p']; // 해상도 필터 (원하면 비워도 됨)
$extPattern = 'm3u8|mpd|ts|mp4|webm|mkv|flv';

// ✅ 정규식 추출 (확장자 + 쿼리 포함)
preg_match_all("/https?:\/\/[^\s'\"<>]+\.($extPattern)(\?[^\s'\"<>]*)?/", $input, $matches);
$urls = array_unique($matches[0]); // ✅ 중복 제거

// ✅ 도메인 + 해상도 필터링
$filtered = array_filter($urls, function($url) use ($allowedDomains, $allowedResolutions) {
    // 도메인 체크
    $domainMatch = false;
    foreach ($allowedDomains as $domain) {
        if (strpos($url, $domain) !== false) {
            $domainMatch = true;
            break;
        }
    }
    if (!$domainMatch) return false;

    // 해상도 체크 (비어있으면 무시)
    if (!empty($allowedResolutions)) {
        foreach ($allowedResolutions as $res) {
            if (stripos($url, $res) !== false) return true;
        }
        return false; // 해상도 못찾음
    }

    return true;
});

// ✅ 결과 출력 (JSON + 텍스트)
$result = array_values($filtered);

echo "🔍 최종 추출된 URL 목록 (" . count($result) . "개):\n";
foreach ($result as $i => $url) {
    echo ($i + 1) . ". $url\n";
}

echo "\n\n📦 JSON 형식 결과:\n";
header('Content-Type: application/json; charset=utf-8');
echo json_encode($result, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
?>
[/code]
📌 출력 예시[code]🔍 최종 추출된 URL 목록 (3개):
1. https://video.com/video/20250517/1bfbde0529c893e16766731f9d42cfaf/index.m3u8
2. https://cdn.example.com/movie_1080p.mp4?token=abc
3. https://cdn.example.com/movie_720p.mp4

📦 JSON 형식 결과:
[
  "https://video.com/video/20250517/1bfbde0529c893e16766731f9d42cfaf/index.m3u8",
  "https://cdn.example.com/movie_1080p.mp4?token=abc",
  "https://cdn.example.com/movie_720p.mp4"
][/code]
🎯 참고 설정 가이드
해상도 필터 제거하려면 $allowedResolutions = [];
도메인 필터 제거하려면 $allowedDomains = []; (또는 조건문 수정)
확장자 추가하려면 $extPattern = 'm3u8|mpd|ts|mp4|webm|mkv|flv|avi|mov'; 등등

댓글목록

등록된 댓글이 없습니다.


자료 목록
번호 제목 날짜
173
🫧
04-17
172
🫧
04-17
171
🫧
04-16
170
🫧
04-15
169
🫧
04-15
168
🫧
04-13
167
🫧
04-13
166
🫧
04-13
165
🫧
04-13
164
🫧
04-13
📖
🫧
04-10
162
🫧
04-10
161
🫧
04-09
160
🫧
04-09
159
🫧
04-08

🔍 검색

회사소개 개인정보처리방침 서비스이용약관
Copyright © rainbowgarden.shop All rights reserved.