2015-05-19 37 views
0

随着preg_replace()的使用,我想将字符串 http://www.vaidehielink.com/更改为www.vaidehielink.com如何在php中preg_replace中设置多个参数

我已经成功地用下面的代码来获取www.vaidehielink.com/结果:

$str = "http://www.vaidehielink.com/"; 
$pattern= '(http://)'; 
$copy_date = preg_replace($pattern, "", $str); 

但我要寻找一个模式除了删除尾随/

+0

是不可能的preg_replace() –

+0

是的,但为什么??? – AbraCadaver

+0

正则表达式对此没有必要。如果你真的想找到一个URL的主机部分,你可以使用PHP的内置'url_parse'函数而不是你自己的代码。 (当然,如果你只是在练习正则表达式,那么继续...) – RJHunter

回答

0

,你是缺少分隔符,只是改变你的模式,你的更换,以这样的:

$pattern= "/^(http:\/\/)(.*?)(\/)?$/"; 
echo $copy_date = preg_replace($pattern, "$2", $str); 

输出:

www.vaidehielink.com 

基本上你只抓取http:///之间的字符串(如果有的话)。

+0

你能解释一下$ pattern =“/^(http:\/\/)(.*?)((//)? $ /“; –

+0

@abhiAhere'^(http:\/\ /)'匹配字符串开头的'http://','(\ /)?$'匹配字符串末尾的'/'0或1次;你只需要抓住这个的中间部分,这是'(。*?)' – Rizier123

+0

@ Rizier123谢谢我是谷歌搜索模式的解释,但没有得到适当的答案。这个答案帮了我很多, –

1

不需要正则表达式。

$copy_date = str_replace(array('http:', '/'), '', $str); 

或者可能是合适的工具:

$copy_date = parse_url($str, PHP_URL_HOST); 
+0

我第一次也是一样。他只想在最后删除斜线。 – Rizier123

+0

好的,你在哪里看到的? – AbraCadaver

+0

*以便尾部的斜线也可以被移除。* < - 这里是 – Rizier123

相关问题