marklarah Posted April 5, 2008 Share Posted April 5, 2008 Okay, so lets say i want to convert a sting of a link (eg; http://www.time.com/time/arts/article/0,8599,1725102,00.html?cnn=yes ) to just "time.com" A the mo', i use $to = "http://www.time.com/time/arts/article/0,8599,1725102,00.html?cnn=yes"; $tom = strstr($to,"www"); echo substr($tom, 0, strpos($tom, "/")) Now, it reruns "www.time.com" But that may just return "http:" if the url was http://example.example.com or whatever. So how to i get rid of http:// ? Link to comment https://forums.phpfreaks.com/topic/99685-how-to-manipulate-strings/ Share on other sites More sharing options...
kenrbnsn Posted April 5, 2008 Share Posted April 5, 2008 Use the str_replace() function <?php $to = "http://www.time.com/time/arts/article/0,8599,1725102,00.html?cnn=yes"; $tom = strstr($to,"www"); echo str_replace('http://','',substr($tom, 0, strpos($tom, "/"))); ?> Ken Link to comment https://forums.phpfreaks.com/topic/99685-how-to-manipulate-strings/#findComment-509971 Share on other sites More sharing options...
laffin Posted April 5, 2008 Share Posted April 5, 2008 $host=parse_url ( $to , PHP_URL_HOST ); will return the hostname than ya can use strstr or preg_replace for the removal of www. portion.I wud opt for preg_replace $host=preg_replace("/^(www|www[0-9])?\.?([\w\.]*)$/i",'\2',$host); Link to comment https://forums.phpfreaks.com/topic/99685-how-to-manipulate-strings/#findComment-509973 Share on other sites More sharing options...
marklarah Posted April 5, 2008 Author Share Posted April 5, 2008 never mind i used this $to = str_replace('http://','', $to); $to = str_replace('www.','', $to); $to = substr($to, 0, strpos($to, "/")); Link to comment https://forums.phpfreaks.com/topic/99685-how-to-manipulate-strings/#findComment-509975 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.