如何替换http://和http://之后的第一个斜杠之间的任何东西


How to replace any thing between http:// and first slash / after http://?

我得到了一个url作为字符串,我想替换http://和第一个斜杠//strong>之间的任何内容(在本例中,我想用video2.de.secondsite.net替换proxy-63.somesite.com)。有人能告诉我如何做到这一点吗?感谢

注意:http://和第一个斜杠之间的数据是动态的,所以我不能只使用替换函数!

 http://proxy-63.somesite.com/sec(dfgghdfdgd987435392429324343241k)/video/600/500/12345678_mp4_h264_aac_2.m3u8
replaced with :
 http://video2.de.secondsite.net/sec(dfgghdfdgd987435392429324343241k)/video/600/500/12345678_mp4_h264_aac_2.m3u8

您可以使用parse_url

$originalUrl = 'http://yourserver';
$parts = parse_url( $originalUrl);
$newServer = 'video2.de.secondsite.net';
$newUrl = $parts['scheme'] . '//' . $newServer . $parts['path'] . '?' . 
          $parts['query'] . '#' . $parts['fragment'];

在盲目添加#和?之前,您可能需要更加小心,并实际测试是否存在查询和片段?

这里有一个JS解决方案,因为我已经把它写下来了,你甚至不需要测试?和#,因为它们是location.hashlocation.search 的一部分

location.protocol + '//' + 'video2.de.secondsite.net' +
location.pathname + location.search + location.hash

每次原始URL都可能不同,preg_match会找到根。

$url = 'http://proxy-63.somesite.com/sec(dfgghdfdgd987435392429324343241k)/video/600/500/12345678_mp4_h264_aac_2.m3u8';
preg_match('@^(?:http://)?([^/]+)@i', $url, $domain);
$host = $domain[1];
$newUrl = str_replace($host, 'video2.de.secondsite.net', $url);
echo $newUrl;