开发者

Need to trasform a link into another with PHP

开发者 https://www.devze.com 2023-03-26 14:10 出处:网络
I have this youtube link for example: http://www.youtube.com/watch?v=3aICB2mUu2k And I need with PHP to transform it in this way: http:开发者_如何学C//www.youtube.com/e/3aICB2mUu2k

I have this youtube link for example: http://www.youtube.com/watch?v=3aICB2mUu2k

And I need with PHP to transform it in this way: http:开发者_如何学C//www.youtube.com/e/3aICB2mUu2k

I need to replace wathc?v= with /e/

How can I do that?

Thanks


$string="http://www.youtube.com/watch?v=3aICB2mUu2k";
$newString=str_replace('/watch?v=','/e/',$string);

str_replace Manual

Added after your comment:
Use strtok

$newString=strtok($newString,'&');


Maybe simpler for you:

$url = 'http://www.youtube.com/watch?v=3aICB2mUu2k';

$url = str_replace('wathc?v=', '/e/', $url);


See preg_replace in the PHP manual: http://php.net/manual/en/function.preg-replace.php


After your comment in RiaD's response, I'd suggest this:

<?php
$url = 'http://www.youtube.com/watch?v=3aICB2mUu2k&feature=related';
parse_str(parse_url($url, PHP_URL_QUERY), $query);
$url = 'http://youtube.com/e/' . $query['v'];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号