Need to trasform a link into another with PHP
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'];
精彩评论