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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.