如何让用户输入视频网址并在提交后在同一页面上显示视频? [英] How do I let a user input a video url and have the video show on the same page upon submit?

查看:156
本文介绍了如何让用户输入视频网址并在提交后在同一页面上显示视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < form id =rp_embed_videoname = rp_embed-videomethod =postaction => 
< div class =rp_block>
< label><?php printf(__('Add Video Link:')); ?>< /标签>
< input type =urlid =rp_newvidname =rp_newvidvalue =required />
< / div>
< div class =rp_block>
< label><?php printf(__('Choose One:'));?>< / label>
< input type =radioname =rp_typevalue =YouTuberequired checked />
< input type =radioname =rp_typevalue =Vimeorequired />
< / div>
< div class =rp_block>
< label><?php _e('Title');?>< / label>
< input type =textname =rp_titlevalue =/>
< / 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;?>< /标签>
< textarea name =rp_desc<?php if(!$ desc_status):?> class =wpvp_require<?php endif;?>>< / textarea>
< / div>
< div class =rp_block>
< div class =rp_catstyle =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_tagstyle =float:right; width:50%; text-align:right;>
< label><?php _e('Tags(comma separated)');?>< / label>
< input type =textname =rp_tagsvalue =/>
< / div>
<?php
}
?>
< / div>
< input type =hiddenname =rp_actionvalue =rp_embed/>
< p class =rp_submit_block>
< input type =submitclass =rp-submitname =rp-embedvalue =Add Video/>
< / p>
< / form>

<?php
if(isset($ _ POST ['rp_newvid'])){
//获取视频网址并将其放入$ video变量
$ videoID = $ _POST ['rp_newvid'];
$ type = $ _POST ['rp_type'];
echo rp_video_embed($ _ POST ['rp_newvid'],'720px','380px',$ _POST ['rp_type']);
}
?>


<?php
函数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 =0allowfullscreen>< / 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('没有找到视频代码')。'< / span> ;';


else {
$ result ='< span style =color:red;>'._ e('视频来源未设定或不支持')。'。< / span>';
}
返回$ result;
}

?>


解决方案

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

这实质上是将来源设为www.youtube.com/embed/www.youtube.com/watch?v=vidID。



您需要解析用户提交的URL,并在插入< iframe>

$之前仅取得vidID值b
$ b

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

下面是如何执行此操作的一个简单示例:

 <?php 
//变量示例
$ youtubeURL ='www。 youtube.com/watch?v=vidID;
$ vimeoURL ='https://vimeo.com/vidID';

//测试值
$ vidURL = $ youtubeURL;
$ videoSource ='youtube';

//选择基于视频源的分隔符
switch($ videoSource){
case'youtube':
$ delimiter ='=';
休息;
case'vimeo':
$ delimiter ='/';
休息;
默认值:
//不管你想要什么回退
}

//隔离vidID值字符串
$ vidID = end(explode($ delimiter, $ vidURL));
?>


I've seen a few similar questions, but none of the answers have worked for me. Ideally, the user would input the video url into the form, hit submit, and then the video would show below the form. Here's my code that's not working (sorry if it's messy or confusing):

<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;
    }

?>

解决方案

It appears that you are asking a user to enter a URL such as "www.youtube.com/watch?v=vidID", then taking that entire string value and inserting it into the <iframe> source value.

This essentially sets the source to "www.youtube.com/embed/www.youtube.com/watch?v=vidID".

You need to parse the URL submitted by the user and take only the vidID value before inserting into the <iframe>

Because of the structure of these URL's the video ID is the last value in the string. This means you could do "explode" the string and take the last value in the array that creates.

Here's a quick example of how you could do this:

<?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));
?>

这篇关于如何让用户输入视频网址并在提交后在同一页面上显示视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆