开发者

how change the string in php?

开发者 https://www.devze.com 2023-03-04 16:11 出处:网络
eg: $url=http开发者_运维知识库://www.example.com/. how to make the $url to this style. http://test.google.com/example.com in php?PHP: Simple and easy way to format URL string should clear everythi

eg:

$url=http开发者_运维知识库://www.example.com/.

how to make the $url to this style.

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


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


$url_parts=parse_url($url);

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


$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


Try this:

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

精彩评论

暂无评论...
验证码 换一张
取 消