0

I'm trying to get the domain and tld from an http_host by using the following regexp:

(?:^|\.)(.+?\.(?:it|com))

The regexp works correctly in gskinner.com.

Input:

domain.com
www.domain.com

Captured groups in both:

domain.com

However in php the following:

preg_match("/(?:^|\.)(.+?\.(?:it|com))/", "www.domain.com", $matches);
print_r($matches);

Output:

Array
(
    [0] => www.baloodealer.com
    [1] => www.baloodealer.com
)

What's wrong with this?

Damiano Barbati
  • 3,356
  • 8
  • 39
  • 51

2 Answers2

1

You can do this:

preg_match("/[^.]+\.(?:it|com)$/", "www.domain.com", $matches);
print_r($matches);

/* output
Array
(
    [0] => domain.com
)
*/
CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
  • I'm giving this to pob because he answered first. But thanks anyway Crayon!! I always use the dot to be faster but it is do darn evil. – Damiano Barbati Nov 08 '13 at 22:03
  • @DamianoBarbati okay well, pob's regex is flawed. run pob's regex against `www.company.com` and see what happens. – CrayonViolent Nov 08 '13 at 22:05
0

I presume you want the last two parts of the domain? Before the TLD, don't match just any character:

"/(?:^|\.)([^\.]+?\.(?:it|com))$/"

(EDIT: anchoring to the end of the string, to fix bug pointed out in comment.)

pobrelkey
  • 5,853
  • 20
  • 29