Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Global or Regex to get name from URL?

Quote Reply
Global or Regex to get name from URL?
Hey Folks,

I've been searching for hours to find a way to do this....

How could I extract the site name from a URL???

For Example...Let's say I have a field containing a URL:

http://www.examplesite.com

I need a global to return:

examplesite

I've searched throught regex tutorials, but can't seem to find a way to cut the name out after after and before the "."

If anyone can help find a solution, or point me in the right direction I would really appriciate it!!!

Crazy Jonze Crazy
Quote Reply
Re: [Jonze] Global or Regex to get name from URL? In reply to
Something like this:

Code:
sub {
my $url = shift;
if($url=~ m/www\.($.)\.com/){
return $1;
}
}

This will not work for URLs whithout www!

Have fun.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Blondies can have brains (sometimes...)
Quote Reply
Re: [SaraBem] Global or Regex to get name from URL? In reply to
Hi, thank you so much for the help. I played with the global quite a bit, and still couldn't get it working. No value is returned, and there are no error messages.

Thanks again!
Quote Reply
Re: [Jonze] Global or Regex to get name from URL? In reply to
Ooops, seems I did a mistake.
Try this one:

Code:
sub {
my $url = shift;
if($url=~ m/www\.(.*?)\.com/){
return $1;
}
}

Some things you can play around:
if($url=~ m/[htpp|https]\:\/\/www\.(.*?)\.[com|net|org]){

you can also check length of array @url (split "." as delimiter) than get the position you want.
Basically you will need $url[1] since $url[0] will be anything like http:\\www., https:\\www. or just http:\\
To prevent wrong results, check if $url[2] is com, net or org.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Blondies can have brains (sometimes...)

Last edited by:

SaraBem: Mar 1, 2007, 2:10 PM
Quote Reply
Re: [SaraBem] Global or Regex to get name from URL? In reply to
Still having no luck, but I appriciate all the help!

At least I'm now heading in the right direction. Guess I need to buy some books to brush up on Perl.
Quote Reply
Re: [Jonze] Global or Regex to get name from URL? In reply to
It should work. Describe how you are trying to use it.

BTW, search Google for "perl regex" or "perl regular expression"

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Blondies can have brains (sometimes...)
Quote Reply
Re: [SaraBem] Global or Regex to get name from URL? In reply to
I'm trying to pull the "URL" field from the Links table just like "http://www.examplesite.com". Then have the global extract the website names have them returned on my detailed pages.

So I need to get rid of "http://www." and then ".com" and return "examplesite". Is that what you mean?

Calling with........... <%cut_url%>

sub {
my $url = shift;
if($url=~ m/www\.(.*?)\.com/){
return $1;
}
}

Quote Reply
Re: [Jonze] Global or Regex to get name from URL? In reply to
Yes, that's what it does.
Sorry, I've been working so many hours that seems I'm doing a lot of mistakes...

Try this:

Code:
sub {
my $url = shift;
$url= $tags->{'URL'};
if($url=~ m/www\.(.*?)\.com/){
return $1;
}
}


This code will get the tag URL, filter it and give you back only examplesite

Save as a Global under any name than put in your templates <%nome of the global%>

Read more about Regex so you can be able to improve the code and even create your own for other purposes too. Regex is not that complicated as it sounds and will help you a lot.

Note: the m in regex can be excluded but preserve the space between ~ and first /

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Blondies can have brains (sometimes...)

Last edited by:

SaraBem: Mar 1, 2007, 4:49 PM
Quote Reply
Re: [SaraBem] Global or Regex to get name from URL? In reply to
Excellent! Thanks a million for all your effort Sara... your a gem!

Here's the complete global for anyone who might find it useful. If your wondering, it can be used for SEO purposes...

Quote:

sub {
my $tags = shift;
my $url = shift;
$url= $tags->{'URL'};
if($url=~ m/www\.(.*?)\.com/){
return $1; }
}
Quote Reply
Re: [Jonze] Global or Regex to get name from URL? In reply to
This global lets you grab the "name" of the domain, and also the TLD (extension)

Code:
sub {
my $url = $_[0];
my $return_full = $_[1];
my ($tld,$domain_name);
if ($url=~ m/www\.(.*?)(\.com|\.co\.uk|\.net|\.org?)/i){
$domain_name = $1;
$tld = $2;
}
$return_full ? return $domain_name . $tld : return $domain_name;
}

Call with: <%global_name($URL,1)%>

This would process <%URL%> - If you have another field, just use $Field_Name

Set the second paramater to "0" to just return the name, or 1 to return the domain and tld.

Enjoy :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Global or Regex to get name from URL? In reply to
Hey Andy!

Nice little global you made there. That's what I wanted to do originally, but didn't want to make things to complicated.


Long time no speak... Hope all is well!

Much thanks