PHP: Remove 'WWW' from URL inside a String

戏子无情 提交于 2019-11-27 15:57:48

问题


Currently I am using parse_url, however the host item of the array also includes the 'WWW' part which I do not want. How would I go about removing this?

$parse = parse_url($url);
print_r($parse);
$url = $parse['host'] . $parse['path'];
echo $url;

回答1:


$url = preg_replace('#^www\.(.+\.)#i', '$1', $parse['host']) . $parse['path'];

This won't remove the www in www.com, but www.www.com results in www.com.




回答2:


preg_replace('#^(http(s)?://)?w{3}\.#', '$1', $url);

if you don't need a protocol prefix, leave the second parameter empty




回答3:


$url = preg_replace('/^www\./i', '', $parse['host']) . $parse['path'];



回答4:


str_ireplace('www.','',$parse['host']); 


来源:https://stackoverflow.com/questions/6336281/php-remove-www-from-url-inside-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!