Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 重写Youtube URL_Php_Youtube_Rewrite_Strip - Fatal编程技术网

Php 重写Youtube URL

Php 重写Youtube URL,php,youtube,rewrite,strip,Php,Youtube,Rewrite,Strip,我有一些YouTube URL存储在数据库中,需要重写 它们以以下格式存储: $http://youtu.be/IkZuQ-aTIs0 我需要把它们重新写成这样: $http://youtube.com/v/IkZuQ-aTIs0 $<?php if ($video['VideoType']){ $echo "<a rel=\"shadowbox;width=700;height=400;player=swf\" href=\"" . $video['VideoType'] .

我有一些YouTube URL存储在数据库中,需要重写

它们以以下格式存储:

$http://youtu.be/IkZuQ-aTIs0
我需要把它们重新写成这样:

$http://youtube.com/v/IkZuQ-aTIs0
$<?php if ($video['VideoType']){
$echo "<a rel=\"shadowbox;width=700;height=400;player=swf\" href=\"" . $video['VideoType'] . "\">View Video</a>";
$}?>
<?php 
    if ($video['VideoType']) {
        $last_slash_position = strrpos($video['VideoType'], "/");
        $youtube_url_code    = substr($video['VideoType'], $last_slash_position);
        echo "<a rel=\"shadowbox;width=700;height=400;player=swf\" 
                 href=\"http://www.youtube.com/".$youtube_url_code."\">
                 View Video</a>";
    }
?>
这些值存储为变量$VideoType

我这样调用变量:

$http://youtube.com/v/IkZuQ-aTIs0
$<?php if ($video['VideoType']){
$echo "<a rel=\"shadowbox;width=700;height=400;player=swf\" href=\"" . $video['VideoType'] . "\">View Video</a>";
$}?>
<?php 
    if ($video['VideoType']) {
        $last_slash_position = strrpos($video['VideoType'], "/");
        $youtube_url_code    = substr($video['VideoType'], $last_slash_position);
        echo "<a rel=\"shadowbox;width=700;height=400;player=swf\" 
                 href=\"http://www.youtube.com/".$youtube_url_code."\">
                 View Video</a>";
    }
?>
$
我如何重写它们


谢谢您的帮助。

您想使用
preg\u replace
功能:

比如:

$oldurl = 'youtu.be/blah';
$pattern = '/youtu.be/';
$replacement = 'youtube.com/v';
$newurl = preg_replace($pattern, $replacement, $string);

您想使用
preg\u replace
功能:

比如:

$oldurl = 'youtu.be/blah';
$pattern = '/youtu.be/';
$replacement = 'youtube.com/v';
$newurl = preg_replace($pattern, $replacement, $string);

您可以使用正则表达式来执行此操作。如果您的数据库中只存储了youtube URL,那么将最后一个斜杠“IkZuQaTIs0”后面的部分放在“IkZuQaTIs0”后面的src属性中就足够了http://www.youtube.com/'.

对于这个简单的解决方案,请执行以下操作:

$http://youtube.com/v/IkZuQ-aTIs0
$<?php if ($video['VideoType']){
$echo "<a rel=\"shadowbox;width=700;height=400;player=swf\" href=\"" . $video['VideoType'] . "\">View Video</a>";
$}?>
<?php 
    if ($video['VideoType']) {
        $last_slash_position = strrpos($video['VideoType'], "/");
        $youtube_url_code    = substr($video['VideoType'], $last_slash_position);
        echo "<a rel=\"shadowbox;width=700;height=400;player=swf\" 
                 href=\"http://www.youtube.com/".$youtube_url_code."\">
                 View Video</a>";
    }
?>

我目前无法测试它,也许您可以尝试测试最后一个斜杠出现的位置等。您还可以查看函数定义:

但是,请注意性能。构建一个脚本,该脚本将处理数据库并转换每个URL,或者在每个条目中存储一个短URL和一个长URL。因为视图中的正则表达式从来都不是一个好主意


更新:对于每个条目,最好只在数据库中存储youtube视频标识符/url代码,因此在本例中,它将是IkZuQ-aTIs0

您可以使用正则表达式来执行此操作。如果您的数据库中只存储了youtube URL,那么将最后一个斜杠“IkZuQaTIs0”后面的部分放在“IkZuQaTIs0”后面的src属性中就足够了http://www.youtube.com/'.

对于这个简单的解决方案,请执行以下操作:

$http://youtube.com/v/IkZuQ-aTIs0
$<?php if ($video['VideoType']){
$echo "<a rel=\"shadowbox;width=700;height=400;player=swf\" href=\"" . $video['VideoType'] . "\">View Video</a>";
$}?>
<?php 
    if ($video['VideoType']) {
        $last_slash_position = strrpos($video['VideoType'], "/");
        $youtube_url_code    = substr($video['VideoType'], $last_slash_position);
        echo "<a rel=\"shadowbox;width=700;height=400;player=swf\" 
                 href=\"http://www.youtube.com/".$youtube_url_code."\">
                 View Video</a>";
    }
?>

我目前无法测试它,也许您可以尝试测试最后一个斜杠出现的位置等。您还可以查看函数定义:

但是,请注意性能。构建一个脚本,该脚本将处理数据库并转换每个URL,或者在每个条目中存储一个短URL和一个长URL。因为视图中的正则表达式从来都不是一个好主意


更新:对于每个条目,最好只在数据库中存储youtube视频标识符/url代码,因此在本例中,它将是IkZuQ-aTIs0

谢谢你的回复。我在“href=”行中遇到此错误。。。解析错误:语法错误,意外的T_常量_封装_字符串抱歉,我不能在这里测试它。好像有个打字错误。尝试使用我编辑的代码谢谢你的回复。我在“href=”行中遇到此错误。。。解析错误:语法错误,意外的T_常量_封装_字符串抱歉,我不能在这里测试它。好像有个打字错误。尝试使用我编辑的代码