ivytony Posted February 10, 2008 Share Posted February 10, 2008 My site allows user to post urls just like digg.com, however, I would like to recognize the url they post by breaking it down. For example, if a user types this url http://www.company.com/news/local/Feb2008/, I would like to know the company.com part. I am wondering how to do this? Since some users may just type www.company.com/news/local/Feb2008/ or company.com/news/local/Feb2008/ or use https url, I am wondering how to deal with these different formats of the same url? use switch-case statement? but how to tokenize the urls? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/90363-how-to-recognize-a-url-posted-by-user/ Share on other sites More sharing options...
trq Posted February 10, 2008 Share Posted February 10, 2008 You might look at parse_url(). Quote Link to comment https://forums.phpfreaks.com/topic/90363-how-to-recognize-a-url-posted-by-user/#findComment-463285 Share on other sites More sharing options...
ivytony Posted February 10, 2008 Author Share Posted February 10, 2008 thank you so much for the hint! Here's my code: <?php $url = 'DEALpigg.com/news/feb2008/latest/?newsid=10000&date=today'; //convert $url to lowercase no matter what $url = strtolower ($url); //check if the url has http or https $scheme = parse_url($url, PHP_URL_SCHEME); if (!$scheme){ $url = 'http://'. $url; // if url has empty scheme, make http default } //check if the url has www $iswww = 'www.'; $poswww = strpos ($url, $iswww); if ($poswww === false){ $newscheme = $scheme .'://'. $iswww; $schemeslash = $scheme .'://'; //convert scheme to http:// or https:// $url = str_replace($schemeslash, $newscheme, $url); } print_r(parse_url($url)); echo $url."<br />"; $url = parse_url($url, PHP_URL_HOST); echo $url. "<br />"; $shorturl = str_replace("www.", "", $url); echo $shorturl; ?> the variable $shorturl will be used for mysql query. do you guys think my code can be more efficient than this? Because I'm a PHP newbie thanks Quote Link to comment https://forums.phpfreaks.com/topic/90363-how-to-recognize-a-url-posted-by-user/#findComment-463351 Share on other sites More sharing options...
sasa Posted February 10, 2008 Share Posted February 10, 2008 try <?php $url = 'company.com'; preg_match('/([^\.\/]+\.[^\/\.]+)((\/)|($))/', $url, $b); echo $domain = $b[1]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/90363-how-to-recognize-a-url-posted-by-user/#findComment-463364 Share on other sites More sharing options...
ivytony Posted February 10, 2008 Author Share Posted February 10, 2008 wow, that's awesome! thank you so much!!! Quote Link to comment https://forums.phpfreaks.com/topic/90363-how-to-recognize-a-url-posted-by-user/#findComment-463367 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.