繁体   English   中英

使用php preg_replace替换字符串

[英]Replace string using php preg_replace

大家好,我知道preg_replace可以用于格式化字符串,但是我需要在有关领域提供帮助,我的url会像这样

http://www.example.com/index.php/
还要删除http,https,ftp .... sites

我想要的是得到结果

example.com/index.php

echo preg_replace("~(([a-z]*[:](//))|(www.))~", '', "ftp://www.example.com");
$url = 'http://www.example.com/index.php/';
$strpos = strpos($url,'.');
$output = substr($url,$strpos+1);
$parts=parse_url($url);
unset($parts['scheme']);
//echo http_build_url($parts);   
echo implode("",$parts);

编辑

要使用http_build_url您需要pecl_http ,可以使用implode作为替代

像这样

  $url = "http://www.example.com/index.php";
  $parts = parse_url($url);
  unset($parts['scheme']);

  echo preg_replace('/^((ww)[a-z\d][\x2E])/i', '', join('', $parts));

输出量

example.com/index.php

范例#2

$url = "http://ww3.nysif.com/Workers_Compensation.aspx";

输出量

nysif.com/Workers_Compensation.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM