유튜브링크에주소가 있을때만 본문에 나타나게 하려합니다

2024-01-22 (월) 09:55:41 2,011

초보입니다

아래코드를 

쓰기에 링크칸에 주소가 입력되었을때만 본문에 나타나게 하려합니다

<?php
            $youtube_key = substr($link,-11,11);
        ?>
            <?php echo
                '<iframe style="width:100%;height:480px;" frameborder="0" src="https://www.youtube.com/embed/'.$youtube_key.'?autoplay=1&rel=0" class="video-frame stopWhenVideoModal"></iframe>'
            ?>

|

답변 2개

첫째링크 사용시

Copy
<?php 

if( isset($view['link']) && array_filter($view['link']) ){

    $youtube_key = '';

    $youtube_key = substr($view['link'][0],-11,11);

    if($youtube_key != ''){

        echo '<iframe style="width:100%;height:480px;" frameborder="0" src="https://www.youtube.com/embed/'.$youtube_key.'?autoplay=1&rel=0" class="video-frame stopWhenVideoModal"></iframe>';

    }

}

?>

안녕하세요. 

아래의 코드를 참고해보세요~

<?php
    $youtube_key = substr($link,-11,11);
    if ($youtube_key) {
        echo '<iframe style="width:100%;height:480px;" frameborder="0" src="https://www.youtube.com/embed/'.$youtube_key.'?autoplay=1&rel=0" class="video-frame stopWhenVideoModal"></iframe>';
    }
?>

또는 

<?php
    $youtube_key = substr($link,-11,11);

    // YouTube 링크인지 확인합니다.
    if (preg_match('/^[a-zA-Z0-9_-]{11}$/', $youtube_key)) {
        echo '<iframe style="width:100%;height:480px;" frameborder="0" src="https://www.youtube.com/embed/'.$youtube_key.'?autoplay=1&rel=0" class="video-frame stopWhenVideoModal"></iframe>';
    }
?>
 

답변을 작성하려면 로그인이 필요합니다.