更改子域名/域名?

时间:2016-06-13 08:38:55

标签: php regex filter

我正在制作一个项目,将google.docs视频作为重定向器在php中删除并将它们放入videojs播放器中(因为doc的当前播放器在flash中)

到目前为止,我所做的一切都很好,但它会像这样撕掉链接

https://r8---sn-vgqs7n7k.c.docs.google.com/videoplayback?requiressl=yes&id=a8fa0f1624209c52&itag=22&source=webdrive&app=texmex&ip=2604:5800:0:35:250:56ff:fe9e:85f5&ipbits=32

我想使用PHP用https:redirector.googlevideo.com/替换整个域/子域,但保留所有内容,就像这样

https://redirector.googlevideo.com/videoplayback?requiressl=yes&id=a8fa0f1624209c52&itag=22&source=webdrive&app=texmex&ip=2604:5800:0:35:250:56ff:fe9e:85f5&ipbits=32

我目前正在使用以下行从链接中替换unicode,但我不知道如何更换第一个域行。有什么想法吗?

$ links = str_replace(数组(' \ u003d',' \ u0026'),数组(' =','&& #39;),$ cat [1]);

1 个答案:

答案 0 :(得分:0)

我使用preg_replace,这将替换https://和第一个斜杠之间的字符串部分:

$link = 'https://r8---sn-vgqs7n7k.c.docs.google.com/videoplayback?requiressl=yes&id=a8fa0f1624209c52&itag=22&source=webdrive&app=texmex&ip=2604:5800:0:35:250:56ff:fe9e:85f5&ipbits=32';

$link = preg_replace('#https://[^/]+#', 'https://redirector.googlevideo.com', $link);
echo $link;

<强>输出:

https://redirector.googlevideo.com/videoplayback?requiressl=yes&id=a8fa0f1624209c52&itag=22&source=webdrive&app=texmex&ip=2604:5800:0:35:250:56ff:fe9e:85f5&ipbits=32

<强>解释

#           : regex delimiter
  https://  : literally https://
  [^/]+     : one or more character that is not a slash
#           : regex delimiter