Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression to match http://anything.extension

Status
Not open for further replies.

DigitalBuilder

Programmer
Apr 7, 2005
33
NZ
This is for what may be a large traffic site in New Zealand [Population 4 Million]

Hello, I wanted a domain to match anything with a domain space i can specify

I have 'http://[\w|\W]{1,}nz' But it doesn't cut off at the .nz

1] How do i make it cut off at the .nz
2] How can i make it return the stripped part?


 
>>But it doesn't cut off at the .nz
where does it cut off???

Known is handfull, Unknown is worldfull
 
but ur RegEx will do just that!!!

Known is handfull, Unknown is worldfull
 
There is still some confusion here and the pattern will fail if the domain name itself contains the letter combination nz, e.g. kenzander.net.nz
Code:
$str = <<<END
[URL unfurl="true"]http://whatever.co.nz[/URL]
[URL unfurl="true"]http://kenzander.net.nz[/URL]
[URL unfurl="true"]http://whatever.gv.nz[/URL]
[URL unfurl="true"]http://dynamic.net.nz?comeon=this&and=that[/URL]
END;

$pattern = "/(\w+\.\w+)\.nz/";
preg_match_all($pattern,$str,$myArr);

# The first subexpression will have the 'stripped' domains
print_r($myArr[1]);

Explanation:
Code:
/    opening regex delimiter
(    start subpattern
\w   any word character
+    one ore more
\.   a literal period - needs escaping since the dot operator has meta meaning
\w+  one or more word chars
)    close subpattern
\.nz the .nz part with the escaped dot
/    end delimiter

Greetings to New Zealand.
 
if you want "anything.nz", then why bother with all this nonsense:
$pattern = "/(\w+\.\w+)\.nz/";

just use "anything.nz":
$pattern = "/(.+)\.nz/";


-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
 
jemminger said:
why bother with all this nonsense
It's fine to disagree, but please don't pass judgements or belittle other posters' contributions.
Nonsense is something that makes no-sense, however, the provided solution works perfectly.

What really bothers me are unprofessional comments in "INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS".
 
now who's the one passing judgements on posts? i posted a figure of speech, YOU read so much into it.

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Certainly I am passing judgement, not on your professional skills, but your choice of figures of speech. ;) No offense intended.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top