0

i would like to have one combined php statement that will strip domain names my clients will enter so its like cnn.com, not http://cnn.com, or www.cnn.com, or ftp.cnn.com or anything else common to domain urls. is it possible, from Stackoverflow i have this code so far

$valueStrip = explode("//",$value);
$value = $valueStrip[1];

that should handle http:// https:// ftp:// but what about the rest and how to get it all in one statement. example please if its possible

user1951739
  • 135
  • 1
  • 9

2 Answers2

1

If your goal is to get just the host name, then what you have will already do that. You should not strip off the "www" or the "ftp" because they are entirely different host names. It's true that in the case of cnn.com they go to the same place, but that is not necessarily always true and you can't rely on that.

However, if your goal is to remove "www." or "ftp.", you can do this:

$valueStrip = explode( "//",$value );
$value = str_replace( array( "www.", "ftp." ), "", $valueStrip[1] );

On the other hand, you might have something like "www.somesite.cnn.com". If you just want to remove the "www.", then the above would work. If you still want to be left only with the top- and second-level portions of the domain, then something like this would be better:

$valueStrip = explode( "//",$value );
$parts = explode( ".", $valueStrip[1] );
$value = $parts[ count( $parts ) - 2 ] . "." . $parts[ count( $parts ) - 1 ];

I don't think either of these are a good idea. Instead, I think sticking with the value that you arrive at in your code with $value is where you should leave it.

Nick Coons
  • 3,682
  • 1
  • 19
  • 21
  • Sorry, I had a typo. $valueStrip was missing the [1] index in the second example. I've tried them both and they both work now. Giving "www.google.com" or "ftp.google.com" to the first one returns "google.com". Giving "www.google.com" or "www.my.site.google.com" to the second one returns "google.com". – Nick Coons Aug 03 '13 at 18:23
  • works, any why to combine the two or a just incase comparison so i always get the right value regardless – user1951739 Aug 03 '13 at 18:43
  • The reason I have two separate examples is because it depends on what you consider to be "the right value". If "the right value" means removing instances of "www." and "ftp." only from the host name, use the first one. If "the right value" means removing everything except the top- and second-level domain parts (which would include removing "www." and "ftp." if they were third-level or greater), then use the second one. Hope that makes sense. – Nick Coons Aug 03 '13 at 18:48
  • Yes , totally, sorry if i sounded rude or unappreciative, i have made a wordpress plugin that takes screen shots of other websites, returns an image and bob's your uncle, problem is for cosmetic purposes coz under the image i would just want to be in uniform and write the domain names without the front part, be it your first example or second. hope that makes me clearer than before. – user1951739 Aug 03 '13 at 18:57
  • You didn't sound rude or unappreciative. I was just trying to make sure that I was being clear that the two examples were intended to be separate rather than work together, so there was no reason to combine them. – Nick Coons Aug 03 '13 at 19:00
0

PHP URL Parsing

$parsed_url = parse_url($value);
$host_only = $parsed_url["host"];
$domain_only = stristr($host_only,".",TRUE);
DevlshOne
  • 8,357
  • 1
  • 29
  • 37