如何让用户输入视频 url 并在提交后在同一页面上显示视频?

How do I let a user input a video url and have the video show on the same page upon submit?

我见过几个类似的问题,但 none 的答案对我有用。理想情况下,用户将视频 url 输入表单,点击提交,然后视频将显示在表单下方。这是我的代码,它不起作用(抱歉,如果它混乱或混乱):

<form id="rp_embed_video" name="rp_embed-video" method="post" action="">
    <div class="rp_block">
        <label><?php printf( __( 'Add Video Link:' ) ); ?></label>
        <input type="url" id="rp_newvid" name="rp_newvid" value="" required />
    </div>
    <div class="rp_block">
        <label><?php printf( __( 'Choose One:' ) );?></label>
        <input type="radio" name="rp_type" value="YouTube" required checked />
        <input type="radio" name="rp_type" value="Vimeo" required />        
    </div>
    <div class="rp_block">
        <label><?php _e('Title');?></label>
        <input type="text" name="rp_title" value="" />
    </div>
    <div class="rp_block">
    <?php $desc_status = (int)get_option('rp_uploader_desc',false);?>
        <label><?php _e('Description');?><?php if(!$desc_status):?><span>*</span><?php endif;?></label>
        <textarea name="rp_desc" <?php if(!$desc_status):?>class="wpvp_require"<?php endif;?>></textarea>
    </div>
    <div class="rp_block">
        <div class="rp_cat" style="float:left;width:50%;">
            <label><?php _e('Choose category');?></label>
            <?php RP_Helper::rp_upload_categories_dropdown();?>
        </div>
        <?php   
        $hide_tags = get_option('rp_uploader_tags','');
        if($hide_tags==''){ ?>
        <div class="rp_tag" style="float:right;width:50%;text-align:right;">
            <label><?php _e('Tags (comma separated)');?></label>
            <input type="text" name="rp_tags" value="" />
        </div>
        <?php   
        } 
        ?>
    </div>
    <input type="hidden" name="rp_action" value="rp_embed" />
    <p class="rp_submit_block">
        <input type="submit" class="rp-submit" name="rp-embed" value="Add Video" />
    </p>
</form>

<?php
   if (isset($_POST['rp_newvid'])) {
       // Get the video URL and put it in the $video variable
        $videoID = $_POST['rp_newvid'];
        $type = $_POST['rp_type'];
    echo rp_video_embed($_POST['rp_newvid'], '720px', '380px', $_POST['rp_type']);
   }
?>


<?php
function rp_video_embed($videoID,$width,$height,$type){
        if($type){
            if($videoID){
                if($type=='YouTube'){
                    $embedCode = '<iframe width="'.$width.'" height="'.$height.'" src="http://www.youtube.com/embed/'.$videoID.'" frameborder="0" allowfullscreen></iframe>';
                }
                elseif($type=='Vimeo'){
                    $embedCode = '<iframe width="'.$width.'" height="'.$height.'" src="http://player.vimeo.com/video/'.$videoID.'" webkitAllowFullScreen mozallowfullscreen allowFullScreen frameborder="0"></iframe>';
                }
                $result = $embedCode;
            }
            else{
                $result = '<span style="color:red;">'._e('No video code is found').'</span>';
            }
        }
        else{
            $result = '<span style="color:red;">'._e('The video source is either not set or is not supported').'.</span>';
        }
        return $result;
    }

?>

您似乎要求用户输入 URL,例如 "www.youtube.com/watch?v=vidID",然后获取整个字符串值并将其插入到 <iframe> 源值中。

这实际上将源设置为 "www.youtube.com/embed/www.youtube.com/watch?v=vidID"。

需要解析用户提交的URL,只取vidID值,然后插入<iframe>

由于这些 URL 的结构,视频 ID 是字符串中的最后一个值。这意味着您可以执行 "explode" 字符串并获取创建的数组中的最后一个值。

这里有一个简单的例子来说明如何做到这一点:

<?php
    //Sample Variables
    $youtubeURL = 'www.youtube.com/watch?v=vidID';
    $vimeoURL = 'https://vimeo.com/vidID';

    //Test Values
    $vidURL = $youtubeURL;
    $videoSource = 'youtube';

    //Select Delimiter based on the video source
    switch ($videoSource) {
        case 'youtube':
            $delimiter = '=';
            break;
        case 'vimeo':
            $delimiter = '/';
            break;
        default:
            //Whatever fallback you want
    }

    //Isolate the vidID value string
    $vidID = end(explode($delimiter, $vidURL));
?>