0 Pluspunkte 0 Minuspunkte

Ich parse Youtube Video Links mit PHP

// youtube video link            
if(parse_url($matches['3'][$i] . $matches['6'][$i], PHP_URL_HOST) == "youtube.com" || parse_url($matches['3'][$i] . $matches['6'][$i], PHP_URL_HOST) == "www.youtube.com") {                
    
    parse_str(parse_url($matches['3'][$i] . $matches['6'][$i], PHP_URL_QUERY), $query_params);

    //$content = str_replace($matches['0'][$i], "<div class='video-container'><iframe src=\"http://www.youtube.com/embed/$query_params[v]/?wmode=transparent\" frameborder=\"0\" allowfullscreen></iframe></div>", $content);                    
    
    //$content = "<script>function loadVideo(e) { alert(e.id); document.getElementById(e.id).style.display = \"hidden\";}</script>";
    
    $rand = rand(0, 9999999999);
    $content = str_replace($matches['0'][$i], "<div class='video-container' id='$rand'><img src=\"http://i2.ytimg.com/vi/$query_params[v]/hqdefault.jpg\" style=\"width:100%\" onClick=\"loadYtVideo('$query_params[v]', $rand)\"></div>", $content);                    

} 

Wie kann ich das für Youtube Shorts machen so das zuerst nur ein Bild gezeigt wird und wenn man darauf klickt lädt das Short von Youtube?

von  

1 Antwort

0 Pluspunkte 0 Minuspunkte

In diesem Code wird überprüft, ob es sich bei dem erkannten Video um ein Shorts handelt, indem nach spezifischen Pfaden wie '/shorts/' oder '/short/' in der URL gesucht wird.

// YouTube-Video-Link, einschließlich Shorts
if (parse_url($matches['3'][$i] . $matches['6'][$i], PHP_URL_HOST) == "youtube.com" || parse_url($matches['3'][$i] . $matches['6'][$i], PHP_URL_HOST) == "www.youtube.com" || strpos($matches['6'][$i], 'youtu.be') !== false) {

    // Extrahiere die Video-ID
    $video_id = '';
    if (strpos($matches['6'][$i], 'youtube.com') !== false) {
        parse_str(parse_url($matches['3'][$i] . $matches['6'][$i], PHP_URL_QUERY), $query_params);
        $video_id = isset($query_params['v']) ? $query_params['v'] : '';
    } elseif (strpos($matches['6'][$i], 'youtu.be') !== false) {
        $video_id = ltrim(parse_url($matches['3'][$i] . $matches['6'][$i], PHP_URL_PATH), '/');
    }

    // Überprüfe, ob es sich um ein YouTube-Shorts-Video handelt
    $is_shorts = false;
    if (strpos($matches['6'][$i], '/shorts/') !== false) {
        $is_shorts = true;
        // Extrahiere die Video-ID aus dem Shorts-Link
        $shorts_path = parse_url($matches['3'][$i] . $matches['6'][$i], PHP_URL_PATH);
        $video_id = ltrim($shorts_path, '/shorts/');
    }

    if ($is_shorts) {
        // Shorts-Video
        $rand = rand(0, 9999999999);
        if (!empty($video_id)) {
            $content = str_replace($matches['0'][$i], "<div class='video-container' id='$rand'><img src=\"http://i2.ytimg.com/vi/$video_id/hqdefault.jpg\" style=\"width:100%\" onClick=\"loadYtVideo('$video_id', $rand)\"></div>", $content);
        } else {
            // Handle error when video ID is empty
            // You can customize this part as per your requirement
            $content = str_replace($matches['0'][$i], "<div class='video-container'>Error: Invalid video link</div>", $content);
        }
    } else {
        // Normales YouTube-Video
        //$rand = rand(0, 9999999999);
        //$content = str_replace($matches['0'][$i], "<div class='video-container'><iframe src=\"http://www.youtube.com/embed/$video_id/?wmode=transparent\" frameborder=\"0\" allowfullscreen></iframe></div>", $content);
        
        $rand = rand(0, 9999999999);
        $content = str_replace($matches['0'][$i], "<div class='video-container' id='$rand'><img src=\"http://i2.ytimg.com/vi/$query_params[v]/hqdefault.jpg\" style=\"width:100%\" onClick=\"loadYtVideo('$query_params[v]', $rand)\"></div>", $content);
                    
    }
}
von (532 Punkte)