Welcome Guest, Not a member yet? Register   Sign In
Check for http://www
#1

[eluser]megabyte[/eluser]
I have a form, and one of the fields allows the user to enter their url.

whether they enter:

http://www.example.com

or

www.example.com

or

example.com


All I want is to capture it as:

example.com


Anyone have a function they want to share?

I'm sure reg ex. would be a better choice than search and replace using str_replace() and regualr expressions are not my strong point.


thanks in advance.
#2

[eluser]gtech[/eluser]
Code:
<?php
class Home extends Controller   {
  function Home(){
    parent::Controller();
  }
  function index() {
    $url = "http://www.test.com";

    preg_match('/[^.]+\.[^.]+$/', $url, $matches);
    echo "$matches[0]";
  }
}
?>

here you go here is an example using preg_match
#3

[eluser]megabyte[/eluser]
thanks, I appreciate the help.

tested it out with
Code:
$url = "http://test.com";

preg_match('/[^.]+\.[^.]+$/', $url, $matches);
echo "$matches[0]";

and got this though.
Code:
http://test.com
#4

[eluser]gtech[/eluser]
the interwebs a wonderfullthing i nicked this:

this allows all sorts of weird and wonderful combinations like www.test.co.uk and removes trailing slashes like test.co.uk/asdad/asdasd/sad/dd
Code:
<?php
class Home extends Controller   {
  function Home(){
    parent::Controller();
  }
  function test() {
    $url = "http://test.com/asda/dsd";

    echo $this->getdomain($url);
  }
  function getdomain($url) {
    preg_match('@^(?:http://)?([^/]+)@i',$url, $matches) ||  preg_match('@^(?:https://)?([^/]+)@i',$url, $matches);
    $host = $matches[1];
    preg_match('/[^.]+\.[^.]+$/', $host, $usHost);
          
            
    if (strlen($usHost[0])>5){
      $output = $usHost[0];
      if (empty($output)) $output = $url;
      return $output;
    } else {
      preg_match('/[^.]+\.[^.]+\.[^.]+$/', $host, $restofWorldHost);
      $output = $restofWorldHost[0];
      if (empty($output)) $output = $url;
      return $output;
    }
  }
}
?>

from here

[url="http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_22819065.html"]http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_22819065.html[/url]

*note I clicked on the link after I turned scripts off on my browser, using noscript in firefox.. so click on it at your own risk.
#5

[eluser]stevek42[/eluser]
I just made this quickie:

Code:
<?php

function decode_url($url)

{
    $match = '!(?:\w+://)?(?:www.)?(?:(.*))?!i';

    preg_match ( $match, $url, $out );
    return $out[1];

}

$urls = array('http://www.test.com', 'www.test.com', 'http://test.com', 'test.com', 'http://sub.test.com');

foreach ($urls as $url)
{
        echo decode_url($url)."\n";
}

?>

Output:

$ php test.php
test.com
test.com
test.com
test.com
sub.test.com
#6

[eluser]megabyte[/eluser]
yes the internet is a wonderous thing, and the members of this forum are even better.

thanks so much.

and hopefully others get to use this too.




Theme © iAndrew 2016 - Forum software by © MyBB