473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

A pattern about detect domain

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
2 1318
pbmods
5,821 Expert 4TB
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
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

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

Similar topics

1
by: NimP | last post by:
Hi,. I'm trying to detect any links that are contained within an html page using eregi pattern matching. I was wondering if there are any pattern matching geniuses out there who could write a...
5
by: Jamie Jackson | last post by:
Two fairly basic questions: I need to supply a method with an array of strings, which will eventually be used to pattern match against another array of strings. 1. Is there a good way to span...
3
by: jason | last post by:
is there a way to set up an array of bits of generic size, cycle through all the possible bit patterns, and detect a sub-pattern within the bit pattern? for cycling through possible patterns: i...
6
by: hb | last post by:
Hi, Would you please tell me how to detect if the client's browser is closed? I need such event to trigger a database modification. Thank you hb
0
by: headware | last post by:
In his book "Patterns of Enterprise Application Architecture", Martin Fowler says that using the Table Module pattern to model the domain logic precludes the use of inheritance and GoF style OO...
10
by: Marcel Hug | last post by:
Hi NG ! For learning work with patterns I would like to add the MVC-Pattern in my little application. On my form (View) I have a TabController with 2 tabs. In each tab I would like to have a...
5
by: Bubba | last post by:
Hi, We have HttpHandlers that can process requests for up to 50 minutes. Those HttpHandlers are running in an app domain inside the DefaultAppPool. We'd like the HttpHandlers to be able to...
4
by: dustin | last post by:
I've been hacking away on this PEP for a while, and there has been some related discussion on python-dev that went into the PEP: ...
1
by: pbd22 | last post by:
i have the pattern that checks for bad email: var pattern=/^()+@()+\.()+()+/; I want to add a check for a domain name: "mycompany" such that: peter@mycompany.com is a match. and, any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.