Regular Expression Help |
[eluser]Eric Barnes[/eluser]
Hi, I am in need of a little help with regular expressions. What I am trying to do is use a regular expression to take the http_host and return just the domain name and the extension. For example take these urls: http://www.mysite.co.uk/mydir/test/index.php http://www.mywwwsite.co.uk/index.php http://subdomain.testsite.com I need to just return: mysite.co.uk mywwwsite.co.uk testsite.com Here is what I am currently trying but it doesn't seem to be working: Code: $host = $_SERVER['HTTP_HOST']; As you can probably see I am not good at all with regular expressions and this is some code I found online.
[eluser]eggshape[/eluser]
I think this will work for you: Code: $pattern = '|^http://[a-z]+\.([a-z]+\..+)/?|'; You don't have to use '/' as a delimiter. I used '|'. In this case, using something else makes it easier to read. BTW, you can always substr() to remove the 'http://' and then explode() the string using '/' if you have trouble with regexp. You'll then have to search for your string with in_array()...this requires you to know beforehand what to search for.
[eluser]Eric Barnes[/eluser]
Alex, Thanks for tips. Unfortunately I didn't get it to work however I did find this function after I posted this which seems to be working. Code: function extract_domain($url){
[eluser]eggshape[/eluser]
GET OUT! It didn't work? haha. I'm so rusty. Ok I tested this one using your examples. You might find it simpler: Code: $pattern = '|^http://(www\.)?(subdomain\.)?([^/]+)|'; or Code: $pattern = '|^http://(?:www\.)?(?:subdomain\.)?([^/]+)|'; |
Welcome Guest, Not a member yet? Register Sign In |