0

eg:

$url=http://www.example.com/.

how to make the $url to this style.

http://test.google.com/example.com in php?

4 Answers 4

3

PHP: Simple and easy way to format URL string should clear everything up for you

Sign up to request clarification or add additional context in comments.

Comments

2
$url_parts=parse_url($url);

echo $url="http://test.google.com/".str_replace('www.','',$url_parts['host']);

5 Comments

Damn, I obviously did too much than needed!
if the url is test.google.com/example.com. can i still get the result as this.test.google.com/example.com
@zhuanzhou:Then you do not need conversion at all. Check host before applying conversation if it is test.google.com. you do not need conversion.
but i must chect it? this is input by the editor. how to check it if has the test.google.com in the url?
after applying parse_url you can if ($url_parts['host'] == 'test.google.com')
1
$url = "http://www.example.com";

$Step1 = str_replace(array("http://", "https://", "www."), "", $url);
$Step2 = explode("/", $Step1);
$newUrl = "http://test.google.com/".$Step2[0];

Basically what I did is replacing any http://, https:// and www. strings from the URL in $url and replace them with a blank string. Then I explode the result of this replace on an '/' character, because there might be an URL given as http://www.test.com/mydir/ so we lose the mydir. If this isn't want you need, skip step 2 and replace $Step2[0] with $Step1 on the last line.

This last line adds the URL you want in $newUrl

Comments

1

Try this:

$url = "http://www.example.com/";
$url = preg_replace("/(?:http:\/\/)?(?:www\.)?([a-z\d-\.]+)\/.*/", "http://test.google.com/$1", $url);

2 Comments

Please post your answer here, don't just link to some XYZ site
Sorry, i just new in stackoverflow, and only learn code syntax

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.