유튜브링크에주소가 있을때만 본문에 나타나게 하려합니다
본문
초보입니다
아래코드를
쓰기에 링크칸에 주소가 입력되었을때만 본문에 나타나게 하려합니다
<?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
안녕하세요.
아래의 코드를 참고해보세요~
<?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>';
}
?>
첫째링크 사용시
<?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>';
}
}
?>