Jump to content

Recommended Posts

Posted

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?

Posted

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)

Posted

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

)

Posted

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)

Posted
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?
Posted
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.
Posted
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.

Posted
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... :D

 

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 :D

Posted
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 :D

 

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!!!

Posted
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 :rolleyes:

Posted
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 :rolleyes:

 

err; so was I - hence smiley in my text!

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...