Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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替换字符串_Php_Regex - Fatal编程技术网

如何用php替换字符串

如何用php替换字符串,php,regex,Php,Regex,我需要替换这种字符串: https://r2---sn-vgqs7nez.c.docs.google.com/ https://r2---sn-testne4.c.docs.google.com/ https://r5---sn-vfghtgh.c.docs.google.com/ 对此 https://redirector.googlevideo.com/ 使用php,请帮助我您可以使用此正则表达式搜索和替换 $pattern = '/^(https:\/\/)[a-zA-Z0-9\-.]

我需要替换这种字符串:

https://r2---sn-vgqs7nez.c.docs.google.com/
https://r2---sn-testne4.c.docs.google.com/
https://r5---sn-vfghtgh.c.docs.google.com/
对此

https://redirector.googlevideo.com/

使用php,请帮助我

您可以使用此正则表达式搜索和替换

$pattern = '/^(https:\/\/)[a-zA-Z0-9\-.]+.docs.google.com\//g';
preg_replace($pattern, 'https://redirector.googlevideo.com/', $yourstring);
证明:

你试过什么吗?我是一个粉丝,你可以试试看,虽然我同意投票结束这个问题,但我完全不同意两个提议的理由。这个问题并不清楚(非常清楚!),答案也不太宽泛(见下面的答案!)。然而,这是一个“请为我编写代码”,所以我投票的方式相反。我投票结束这个问题作为离题,因为它没有试图解决问题本身,而只是要求一个解决方案。例如,我有这个代码:$headers=get_headers(“”,1);echo$headers['Location'];在结果(位置url)中,我需要将r2--sn-vgqs7nez.c.docs.google.com(以及所有可变url)替换为redirector.googlevideo.com帮助我@SandroMaglie Eric刚刚为您提供了一个“有效”的解决方案。如果你不知道如何复制和粘贴,这里有一个更大的问题。