Web Development Thread, Trim URL with PHP in Coding and Web Development; I have a form where a user can submit a web address, in the following format:
10th February 2009, 11:20 AM #1
Trim URL with PHP
I have a form where a user can submit a web address, in the following format:
I want to trim the url so it looks like this:
I have used parse_url with PHP_URL_HOST which gives me
I just need to remove the further 'www' or '123' or whatever may be there in the prefix for that matter.
IDG Tech News
10th February 2009, 11:39 AM #2
you can do a string search for the first occurrence of the . and trim from ther to the end of the string
the strpos function will find the first dot and the value returned can be used with substr to return the bit you want
try $stripped = substr($url,strpos($url,".")+1)
10th February 2009, 11:55 AM #3
Just be careful as to what format you think a URL should appear (before trying to parse it).
How to Obscure Any URL makes interesting reading!
10th February 2009, 12:03 PM #4
- Rep Power
How about explode(".",$url)?
I too, thought of strrpos to search backwards, but .coms will have 1 dot and co.uks etc will have 2.
I've struggled with this for a content management routine I provided for a friend of mine. It's tough to parse his updates and find the urls and make them active when displayed on the site.
Have you looked at the full array returned by parse_url, the manual shows a lot of information can be returned...
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
10th February 2009, 12:08 PM #5
you could us substr_count to see how many dots there are and then decide where to truncate
10th February 2009, 12:14 PM #6
Hmmm, doesn't seem like a simple fix then?
10th February 2009, 12:27 PM #7
strpos() will find the 1st occurrence, so if you only want to remove the prefix, it should work fine.
10th February 2009, 12:29 PM #8
strpos was my suggestion...seems easy enough to me
10th February 2009, 12:29 PM #9
But in the instance when somebody submits what will happen? I'll be left with won't I?
Originally Posted by Gerry
10th February 2009, 12:41 PM #10
What I started thinking was that you need to make sure that there are at least 2 full stops left before you remove anything - that deals with "google.com" - it has 1 full stop, you don't strip any more. Sadly, it fails with bbc.co.uk - it has 2 full stops so you remove to get co.uk
The only thing I can think of is to work back from the end and do an nslookup on each name that you get; once you can resolve, you know you've got a name.
eg - you're given Google so you try "com" and it fails; you try "google.com" and it works
Not sure how you do name lookups in PHP (I can do it in ASP.Net but that doesn't really help :-)) but I'm sure it must be there (everything else is)
10th February 2009, 12:42 PM #11
- Rep Power
use strrpos, that counts backwards and only slice if you find >1 if .com or >2 if co.uk etc. That's the problem I found, what is the domain and how many dots does it contain?
10th February 2009, 12:47 PM #12
I'm wondering why you would want to do this in the first place - mainly for the reasons mentioned. www.site.com might be the only hostname that works - site.com might not have an appropriate record configured. And what has also been said is that it is very hit and miss as to the results you will get back from splitting, exploding, strpos'ing etc.
10th February 2009, 12:53 PM #13
I have a database that staff can add sites to, by giving an address. The site is stored exactly as they enter it
Originally Posted by webman
and then is sent to a text file stripped of any rubbish
so that our whitelist unblocks it. The reason I want to unblock everything for bbc.co.uk is because styles and images are stored under a different prefix (as there are for many sites nowadays) and without this the sites dont display properly.
10th February 2009, 12:54 PM #14
10th February 2009, 01:04 PM #15
ah so now you have changed the question...
you originally said urls were entered as www.site.com etc so counting from the front would work...
By kerlj001 in forum EduGeek Joomla 1.0 Package
Last Post: 10th December 2008, 10:08 PM
By kennysarmy in forum Windows
Last Post: 16th January 2008, 01:21 PM
By richard.thomas in forum Network and Classroom Management
Last Post: 2nd November 2007, 11:27 AM
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)