Hightower Posted February 10, 2009 Posted February 10, 2009 I have a form where a user can submit a web address, in the following format: http://www.site1.com http://prefix.site1.com http://123.site1.com I want to trim the url so it looks like this: site1.com I have used parse_url with PHP_URL_HOST which gives me www.site1.com or 123.site1.com I just need to remove the further 'www' or '123' or whatever may be there in the prefix for that matter. Any ideas?
CESIL Posted February 10, 2009 Posted February 10, 2009 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)
Martin Posted February 10, 2009 Posted February 10, 2009 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! mb
PaulBM Posted February 10, 2009 Posted February 10, 2009 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... Array ( [scheme] => http [host] => hostname [user] => username [pass] => password [path] => /path [query] => arg=value [fragment] => anchor )
CESIL Posted February 10, 2009 Posted February 10, 2009 you could us substr_count to see how many dots there are and then decide where to truncate
Hightower Posted February 10, 2009 Author Posted February 10, 2009 Hmmm, doesn't seem like a simple fix then?
Gerry Posted February 10, 2009 Posted February 10, 2009 strpos() will find the 1st occurrence, so if you only want to remove the prefix, it should work fine. http://www.w3schools.com/PHP/func_string_strpos.asp
CESIL Posted February 10, 2009 Posted February 10, 2009 strpos was my suggestion...seems easy enough to me
Hightower Posted February 10, 2009 Author Posted February 10, 2009 strpos() will find the 1st occurrence, so if you only want to remove the prefix, it should work fine. PHP strpos() Function But in the instance when somebody submits http://google.com what will happen? I'll be left with .com won't I?
srochford Posted February 10, 2009 Posted February 10, 2009 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)
PaulBM Posted February 10, 2009 Posted February 10, 2009 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?
webman Posted February 10, 2009 Posted February 10, 2009 I'm wondering why you would want to do this in the first place - mainly for the reasons mentioned. http://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.
Hightower Posted February 10, 2009 Author Posted February 10, 2009 I'm wondering why you would want to do this in the first place I have a database that staff can add sites to, by giving an address. The site is stored exactly as they enter it www.bbc.co.uk/sport/blah and then is sent to a text file stripped of any rubbish bbc.co.uk 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.
powdarrmonkey Posted February 10, 2009 Posted February 10, 2009 This is dead easy with a regular expression. See Split an URL into protocol, site, and resource parts - PHP - Snipplr for an example.
CESIL Posted February 10, 2009 Posted February 10, 2009 ah so now you have changed the question... you originally said urls were entered as http://www.site.com etc so counting from the front would work...
Hightower Posted February 10, 2009 Author Posted February 10, 2009 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... Yeah, but we also should know how idiot proof the system has to be if it will be used by teachers - and this should have been taken into consideration as standard by anyone who has replied
srochford Posted February 10, 2009 Posted February 10, 2009 Yeah, but we also should know how idiot proof the system has to be if it will be used by teachers - and this should have been taken into consideration as standard by anyone who has replied Well, if you're going to have a dig at teachers, let's have a dig at IT people who can't phrase a question which includes anything approaching the correct info :-) - nothing in your original post suggested that you were accepting full URLS and your examples were just host names - nothing included the page etc that might be submitted. You'd go down a storm writing examples for open source docs which are all too often full of this kind of half example!!!
powdarrmonkey Posted February 10, 2009 Posted February 10, 2009 and this should have been taken into consideration as standard by anyone who has replied I did.
Hightower Posted February 10, 2009 Author Posted February 10, 2009 Well, if you're going to have a dig at teachers, let's have a dig at IT people who can't phrase a question which includes anything approaching the correct info :-) - nothing in your original post suggested that you were accepting full URLS and your examples were just host names - nothing included the page etc that might be submitted. You'd go down a storm writing examples for open source docs which are all too often full of this kind of half example!!! Whoa whoa, steady on. Might do yourself some damage getting all excited like that. Note the use of my smilie's to show, and I'm gonna highlight this, I WAS HAVING A BIT OF A JOKE. You ought to be careful with your posts, might ruin someones day
srochford Posted February 11, 2009 Posted February 11, 2009 Whoa whoa, steady on. Might do yourself some damage getting all excited like that. Note the use of my smilie's to show, and I'm gonna highlight this, I WAS HAVING A BIT OF A JOKE. You ought to be careful with your posts, might ruin someones day err; so was I - hence smiley in my text!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now