|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
logic - Remove http:// from URL
Hey guys
Just been looking around for the best method to remove http:// or https:// from the BEGINNING of a string. IE: Should not remove the http:// from this: http://www.hi.com/test.php?url=http://www.whatever.com Of course there are 100 ways to skin a cat... But interested to hear what your opinions are on the best way to do this.
__________________
- Richie |
|
#2
|
|||
|
|||
|
I would probably do something like this
PHP Code:
|
|
#3
|
|||
|
|||
|
I would probably use a regular expression also, but simplify what matthew put by matching 0 or 1 of the "s" using the question mark. Also don't forget the i flag to make it case insensitive.
PHP Code:
|
|
#4
|
|||
|
|||
|
ah yeh... well very similar to what I am currently using.. A mate showed me some time ago, never really thought much about it... But I was using it this morning and thought 'hm wonder how others do it'
Code:
preg_replace("/^https?:\/\/(.+)$/i","\\1", $url)
|
|
#5
|
|||
|
|||
|
regexes are really handy. I first started thinking like "oh, whats some way I can do it without a regex" because regexes are sometimes slower than the alternative because its using more power than needed. I just decided though that it would be much easier to do the regex simply because the other way would probably be to use substr with IF statements and scenarios for both https and http and it just wasn't any easier.
Also as a side note, I'm sure there would be maybe a millisecond difference, but the expressions that matthew and I both came up with would be slightly faster than what you put. with ours we are just matching the first part of the string and replacing that with blank. with yours it has to match the string and then sub match everything after http:// and replace that sub match with itself without the first part. like I said above though, it probably wouldn't make much of a difference. |
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > logic - Remove http:// from URL |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|