Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Remove 'WWW' from URL inside a String

Tags:

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;
like image 383
ritch Avatar asked Jun 13 '11 20:06

ritch


People also ask

How to Remove www from URL in PHP?

Remove http://, www., and slashes from the URL First is trim() function, use for remove all slash from the URL. Second is preg_match() function, check http:// or https:// existed in URL. If yes then no need to http:// prepend otherwise you have to http:// prepend to URL. Third is parse_url() function, to parse the URL.

How to Remove http from string in PHP?

$removeChar = ["https://", "http://", "/"];

How to Remove link in text in PHP?

Solution. Use strip_tags( ) to remove HTML and PHP tags from a string: $html = '<a href="http://www.oreilly.com">I <b>love computer books. </b></a>'; print strip_tags($html); I love computer books.


1 Answers

$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.

like image 134
Floern Avatar answered Oct 06 '22 21:10

Floern