By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,083 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,083 IT Pros & Developers. It's quick & easy.

A pattern about detect domain

P: 5
Domain:
http://hk.yahoo.com

pattern:
"/:\/\/(.*)\.[a-z]{2,4}\//"

code:
ereg("/:\/\/(.*)\.[a-z]{2,4}\//", 'http://hk.yahoo.com', $domain);

target:
hk.yahoo

question:
i can not get it.
Dec 22 '07 #1
Share this Question
Share on Google+
2 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Mingkin.

First thing I would do is use preg_match(), as it's faster than ereg(), and it will make your life easier because you can specify your own delimiters:

Expand|Select|Wrap|Line Numbers
  1. preg_match('#://(.+)\\.[a-z]{2,4}#', $str, $matches);
  2. print_r($matches);
  3.  
This code looks for '://', then any number of characters followed by '.', then between 2 and 4 characters. The .+ is greedy, so it will match as many characters as possible.

Note that the '\' is escaped in the string above. You should always be careful when escaping characters in a regular expression. For example, if you pass '\/\/' to preg_match(), PHP will 'escape' the '/' characters, so preg_match() still sees '//', which causes a compile error. Instead, you need to do this: '\\/\\/' so that the '/' is still escaped after PHP is done with it.
Dec 22 '07 #2

P: 5
Hi, pbmods! Thanks you!
Your answer is very detail and useful, i can achieve my target.

Heya, Mingkin.

First thing I would do is use preg_match(), as it's faster than ereg(), and it will make your life easier because you can specify your own delimiters:

Expand|Select|Wrap|Line Numbers
  1. preg_match('#://(.+)\\.[a-z]{2,4}#', $str, $matches);
  2. print_r($matches);
  3.  
This code looks for '://', then any number of characters followed by '.', then between 2 and 4 characters. The .+ is greedy, so it will match as many characters as possible.

Note that the '\' is escaped in the string above. You should always be careful when escaping characters in a regular expression. For example, if you pass '\/\/' to preg_match(), PHP will 'escape' the '/' characters, so preg_match() still sees '//', which causes a compile error. Instead, you need to do this: '\\/\\/' so that the '/' is still escaped after PHP is done with it.
Dec 23 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.