YouTubeのURL入力から埋め込みコードを取得しようとしています。私はこの答えに出くわしましたが、今ではその答えを自分のコードに適用したので、結果が得られません。私の目標は、正規表現でURLを次のようにすることです:http ://www.youtube.com/watch?v=videoid 。正規表現関数を実際にコードで機能させるにはどうすればよいですか?
<html>
<form method="post" action="">
URL:
<input type="text" value="<?php $text ?>" name="yurl"> <!--$vari-->
<br>
<!--<br>
Height:
<input type="text" value="<?php $hth ?>" name="yheight"> $hth
<br>-->
<!--<br>
Width:
<input type="text" value="<?php $wdth ?>" name="ywidth"> $wdth
<br>
<br>-->
Autoplay:
<input type="checkbox" value="<?php $auto1; $auto2; ?>" name="yautop"> <!--$auto1 $auto2-->
<br>
<input type="submit" value="Generate Embed Code" name="ysubmit">
<br>
</form>
</html>
PHP
<?php
$vari ='';
if($_POST)
{
function linkifyYouTubeURLs($vari) {
$vari = preg_replace('~
# Match non-linked youtube URL in the wild. (Rev:20111012)
https?:// # Required scheme. Either http or https.
(?:[0-9A-Z-]+\.)? # Optional subdomain.
(?: # Group host alternatives.
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com followed by
\S* # Allow anything up to VIDEO_ID,
[^\w\-\s] # but char before ID is non-ID char.
) # End host alternatives.
([\w\-]{11}) # $1: VIDEO_ID is exactly 11 chars.
(?=[^\w\-]|$) # Assert next char is non-ID or EOS.
(?! # Assert URL is not pre-linked.
[?=&+%\w]* # Allow URL (query) remainder.
(?: # Group pre-linked alternatives.
[\'"][^<>]*> # Either inside a start tag,
| </a> # or inside <a> element text contents.
) # End recognized pre-linked alts.
) # End negative lookahead assertion.
[?=&+%\w-]* # Consume any URL (query) remainder.
~ix',
'<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',
$vari);
return $vari;
}
$vari = $_POST['yurl'];
$hth = 300; //$_POST['yheight'];
$wdth = 500; //$_POST['ywidth'];
$is_auto = 0;
$step1 =explode ('v=', $vari);
$step2 =explode ('&',$step1[1]);
if(isset($_POST['yautop'] ))
{
if($_POST['yautop'] == 1)
$is_auto = 1;
}
$auto1 = '?autoplay='.$is_auto;
$auto2 = '&autoplay='.$is_auto;
?>
<p>Iframe Result:</p>
<?
//Iframe code with optional autoplay
echo ('<iframe src="http://www.youtube.com/embed/'.$step2[0].'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>');
?>