1

Hi all i know preg_replace can be used for formatting string but i need help in that concerned area my url will be like this

http://www.example.com/index.php/
also remove the http,https,ftp....sites also

what i want is to get result as

example.com/index.php

3
  • Are you fetching these url from DB? Commented Apr 22, 2011 at 10:49
  • 2
    Removing the protocol is easy. But why would you want to remove the subdomain? That changes the URL. Commented Apr 22, 2011 at 10:56
  • Is there really a need to remove subdomain? Can input be vary it always contain a subdomain or sometime url comes without subdomain? Commented Apr 22, 2011 at 11:05

4 Answers 4

1
echo preg_replace("~(([a-z]*[:](//))|(www.))~", '', "ftp://www.example.com");
Sign up to request clarification or add additional context in comments.

1 Comment

@vishnu : check the answer now. It is working all combination as above. Is there any variation in www also
0
$url = 'http://www.example.com/index.php/';
$strpos = strpos($url,'.');
$output = substr($url,$strpos+1);

2 Comments

What exactly will the input format of the url be? Your question has only one example format.
Also, you need to trim the / from the end (if you want the format specified by OP).
0
$parts=parse_url($url);
unset($parts['scheme']);
//echo http_build_url($parts);   
echo implode("",$parts);

EDIT

To use http_build_url you needs pecl_http you can use implode as alternate

1 Comment

It should be noted that ones needs pecl_http for this.
0

Something like this

  $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));

Output

example.com/index.php

Example #2

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

Output

nysif.com/Workers_Compensation.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.