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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to remove www from the url 1

Status
Not open for further replies.

diezy

Technical User
Sep 2, 2002
50
CA
i was wondering how to remove the "www" from a url if it was entered by a user, however still keeping the arguments/url path in tact

 
Here is how I'd do it.

Code:
my $url = "[URL unfurl="true"]http://www.test.com";[/URL]
my $url_copy = $url; # just incase you want the original

$url =~ s/[URL unfurl="true"]www\./\./gi;[/URL]
print $url;
 
If you need a more specific kind of removement (i.e. what if the site was (that site actually exists by the way but prefers to be called ;)))

So here are a couple ways to do that.

Code:
my $url = "[URL unfurl="true"]http://www.test.com";[/URL]

# Don't do a global replace,
# ie like iluveperl's code but without the g
$url =~ s/[URL unfurl="true"]www\./\./i;[/URL]

# Include a protocol in the regexp. This
# way is probably better, because if $url
# was "[URL unfurl="true"]http://www.www.com/"[/URL] then it would
# become [URL unfurl="true"]www.com,[/URL] but if the url was just
# "[URL unfurl="true"]http://www.com"[/URL] then it becomes just ".com"
$url =~ s~^(http|https|ftp)://www\.~\.~i;
 
There are times even that match would fail though.

I guess $url =~ s/ would be better, it'd only s// the first instance.
 
I'm wondering how the user is entering the URL to get it to the script to replace the www. I would assume a form field, in which case you would want to check for valid url syntax to begin with then remove the the proper place in the url string if necessary.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top