我有一个提交表单,用于在我的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网址并将其更改为嵌入代码并将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
from代码并将其放入$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值以使用preg匹配src和ff提取src,否则它应该使用第一个函数。
检查是否存在SRC值,而不是使用......
$url3 = preg_match('~iframe.*src="([^"]*)"~', $url2, $result);
$url4 = $result[1];
除此以外...
$url4 = $url2;
<?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>