473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to add "http" to the beginning of bad links?

Hi,

I have a scalar variable, "$url" that may or not contain "http://" in
front of it. If it is not there, I'd like to add it. Unfortunately,
this code isn't working for me with PHP 4.

if (strpos(strtolower($url), "http://") != 0) {
$url = "http://$url";
} // if

How can I correct?

Thanks, - Dave

May 15 '06 #1
3 2234
NC
la***********@zipmail.com wrote:

I have a scalar variable, "$url" that may or not contain "http://"
in front of it. If it is not there, I'd like to add it. Unfortunately,
this code isn't working for me with PHP 4.

if (strpos(strtolower($url), "http://") != 0) {
$url = "http://$url";
} // if

How can I correct?


if (strpos(strtolower($url), "http://") !== false) {
$url = "http://$url";
}

Read the Manual, it's all there:

http://www.php.net/strpos

Cheers,
NC

May 15 '06 #2
Rik
NC wrote:
la***********@zipmail.com wrote:

I have a scalar variable, "$url" that may or not contain "http://"
in front of it. If it is not there, I'd like to add it.
Unfortunately, this code isn't working for me with PHP 4.

if (strpos(strtolower($url), "http://") !=
0) { $url = "http://$url";
} // if

How can I correct?


if (strpos(strtolower($url), "http://") !== false) {
$url = "http://$url";
}


It's just a pity for relative links, other protocols (https://, ftp://, even
mailto:, all extremely valid). Then again, that wasn't the op's question...

I'd:
if(preg_match('"^(https?://|ftp://|mailto:|\.{0,2}/|smb://|file://|s?news:|l
dap(i|s)?:|gopher:|nntp:|telnet:|mms:|ssh:)"si', $url)<1) $url =
'http://'.$url;

I'm still forgetting a lot of other possibilities though...

Grtz,
--
Rik Wasmus
May 15 '06 #3
Rik:
if(preg_match('"^(https?://|ftp://|mailto:|\.{0,2}/|smb://|file://|s?news:|l
dap(i|s)?:|gopher:|nntp:|telnet:|mms:|ssh:)"si', $url)<1) $url =
'http://'.$url;

I'm still forgetting a lot of other possibilities though...


Forget about particular schemes and match scheme names (plus colon)
in general:

`^[a-z][a-z\d+.-]+:`i

http://www.ietf.org/rfc/rfc3986.txt :3.1

--
Jock

May 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Manjesh Gowda | last post by:
Hi all, I am maintaing a persistent HTTP connection using the "HTTP CONNECT" method , this works fine for many proxies like Squid, ISA , etc. But the iPlanet proxy disconnects the conneciton...
24
by: sinister | last post by:
After doing a websearch, it appears that it's OK to omit the "http:" to form a relative URL. Are there any pitfalls to this? For example, if there is a page http://www.domain1.com/page1.html...
7
by: ryan.fairchild | last post by:
Ok I basically am creating a container for a bunch of other <div> tags. But I want to make four jpegs/gif/whatever that goes in each corner so they look rounded. I thought this is how I would do...
5
by: Studio P.M. | last post by:
Having built successfully my first HTTP Module prototype (as for Q307996 - HOW TO: Create an ASP.NET HTTP Module ...) without really understanding what I've done, now I kindly ask this community...
0
by: Joeyej | last post by:
Hi - I'm trying to move/use a web form (containing some javascript field checks) previously hosted on a Windows 2000 server. However, the FORM METHOD="post..." command in the form (shown below)...
31
by: Yeah | last post by:
Is it absolutely necessary to include "http://" in an A HREF hyperlink? Would it be wise to remove this from one's Links page, just to save code?
3
by: Lee Roth | last post by:
I am testing a web app that links to user defined URLs. I want the link to be the exact text the user provided but I can't seem to get rid of the "http://localhost" prefix in the link. Note that I...
11
by: jjw92 | last post by:
I've been banging my head against the wall with this one for a couple days so I'm hoping someone has some ideas. I have a web service that I created that is called by a .NET class library (which in...
2
by: Tamer | last post by:
Hi! Is there a way to change the protocoll type through Javascript without reloading (or making a request from the webserver) via Javascript? for example: http://mydomain.com...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.