我有一个提交表单,用于在我创建此代码的wordpress网站上添加视频...
<?php
$url1 = get_post_meta( $post->ID , 'video_play' , true );
$search = '#(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x';
$replace = 'http://www.youtube.com/embed/$2';
$url = preg_replace($search,$replace,$url1);
?>
<iframe width="560" height="315" src="<?php echo $url; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
此代码将使用youtube url并将其更改为嵌入代码,并将src添加到iframe,我也可以从可嵌入源发布src源,它也可以工作,但是...
我现在想做的是在字段中输入
<iframe width="560" height="315" src="https://www.youtube.com/embed/videoid" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
从上面的代码中提取https://www.youtube.com/embed/videoid
,并将其放在$url
中。
当我用现有代码尝试时,我的输出是...
<iframe width="560" height="315" src="<iframe width=" iframe-embed"="" scrolling="no" frameborder="0" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
更新: 我发现了如何通过此函数提取嵌入代码的方式...
<?php
$url1 = get_post_meta( $post->ID , 'video_play' , true );
$search = '#(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x';
$replace = 'http://www.youtube.com/embed/$2';
$url2 = preg_replace($search,$replace,$url1);
$url3 = preg_match('~iframe.*src="([^"]*)"~', $url2, $result);
$url4 = $result[1];
?>
<iframe width="560" height="315" src="<?php echo $url4; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
现在,我需要结合第一个功能和第二个功能,php代码应检查其具有SRC值的嵌入代码是否对src和pref匹配src使用preg匹配,否则应使用第一个功能。
类似于检查SRC值是否存在,然后使用...
$url3 = preg_match('~iframe.*src="([^"]*)"~', $url2, $result);
$url4 = $result[1];
否则...
$url4 = $url2;
答案 0 :(得分:0)
<?php
$url1 = get_post_meta( $post->ID , 'video_play' , true );
$search = '#(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x';
$replace = 'http://www.youtube.com/embed/$2';
$url2 = preg_replace($search,$replace,$url1);
$url3 = '<iframe width="560" height="315" src="'.$url2.'" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';
$url4 = preg_match('~iframe.*src="([^"]*)"~', $url3, $result);
$url4 = $result[1];
?>
<iframe width="560" height="315" src="<?php echo $url4; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>