Jump to content

how to recognize a url posted by user?


ivytony

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/90363-how-to-recognize-a-url-posted-by-user/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.