PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old October 29th, 2009, 08:34 PM
wilorichie wilorichie is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Brisbane
Posts: 324 wilorichie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 15 h 29 m 24 sec
Reputation Power: 3
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

Reply With Quote
  #2  
Old October 29th, 2009, 09:02 PM
MatthewJ MatthewJ is offline
Contributing User
Click here for more information.
 
Join Date: May 2007
Location: Davenport, Iowa
Posts: 564 MatthewJ User rank is Private First Class (20 - 50 Reputation Level)MatthewJ User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 21 h 23 m 45 sec
Reputation Power: 3
I would probably do something like this

PHP Code:
<?php
$test 
"http://www.example.com?a=http://www.google.com";
$output preg_replace("/^(http:\/\/|https:\/\/)/"""$test);
echo 
$output;
?>

Reply With Quote
  #3  
Old October 29th, 2009, 11:56 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
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:
<?php 
$test 
"http://www.example.com?a=http://www.google.com"
$output preg_replace("/^https?:\/\//i"""$test);
echo 
$output
?>

Reply With Quote
  #4  
Old October 30th, 2009, 12:45 AM
wilorichie wilorichie is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Brisbane
Posts: 324 wilorichie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 15 h 29 m 24 sec
Reputation Power: 3
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)

Reply With Quote
  #5  
Old October 30th, 2009, 02:25 AM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
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.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > logic - Remove http:// from URL


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek