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, 07:34 PM
wilorichie wilorichie is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Brisbane
Posts: 332 wilorichie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 17 h 6 m 5 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, 08:02 PM
MatthewJ MatthewJ is offline
Contributing User
Codewalkers Novice (500 - 999 posts)
 
Join Date: May 2007
Location: Davenport, Iowa
Posts: 633 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 1 Day 22 h 11 m 38 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, 10:56 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 2,070 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 6 Days 8 h 15 m 37 sec
Reputation Power: 5
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 29th, 2009, 11:45 PM
wilorichie wilorichie is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Brisbane
Posts: 332 wilorichie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 17 h 6 m 5 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, 01:25 AM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 2,070 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 6 Days 8 h 15 m 37 sec
Reputation Power: 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.

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!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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




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