0

I have a long long text contains multiple url. BUT there is only one url which is of blogspot.com . That url will be like http://xxxxx.blogspot.com . How could i get the value of xxxx and store in a variable.

So if I have the whole text store in $foo. Can any one write the code to extract the subdomain out.

I guess it should be of only 1 or 2 line using preg_match . But i'am not getting it to work. Rejex are confusing me.

Thank you

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
cjmling
  • 6,896
  • 10
  • 43
  • 79
  • write the code for you? heh... You should put into it at least a SMALL effort and then someone might help – Zoltan Toth Jan 17 '12 at 17:14
  • Rather than `preg_match()` start with `parse_url()` http://php.net/manual/en/function.parse-url.php and `explode()` the domain part on the dots, then extract the first from the resultant array. – Michael Berkowski Jan 17 '12 at 17:15
  • @zoltan : sorry that i didn't put the code up there.. I did put effort googled, tried. but its just that i didn't put my messed up code up there. **ok any way next time i put some code to show my effort** Thanks You :) and thanks Michael for parse_url suggestion :) – cjmling Jan 17 '12 at 18:23

1 Answers1

0

Try something like this:

$foo = 'http://xxxxx.blogspot.com';
preg_match( '%http://(\w+).blogspot.com%', $foo, $matches);
var_dump( $matches[1]);

This will print string(5) "xxxxx"

Demo

nickb
  • 59,313
  • 13
  • 108
  • 143
  • Only problem I can see with thing is that `\w` doesn't match dashes, which are valid in domain names, and does match underscores, which are not. – DaveRandom Jan 17 '12 at 17:22