amuck-landowner

Need regex help.

KuJoe

Well-Known Member
Verified Provider
So I am trying to find a proper way to use regex in PHP to find if a variable contains "http:/" but not "http://". Basically I have a script that in some rare instances one of the slashes gets removed from a URL so I need to check to see if the slash got removed and add it back if it did.

Any help is appreciated (I suck at regex and Google hasn't been very helpful). Here's the code I have to find "http://" in a variable:

Code:
preg_match("#https?://#", $url)
 

AshleyUK

New Member
Verified Provider
In your post your talking about http://, and in your script your talking about https:// by the looks of it.

Which or both are your looking for?

However this should do the trick:


if (!preg_match("~^(?:ht)tps?://~i", $url)) {
"Does not contain http://"
}
,Ashley
 
Last edited by a moderator:

KuJoe

Well-Known Member
Verified Provider
I fixed the part of the script that removed the 2nd slash sometimes (mod_rewrite is even more confusing than regex for me sometimes). :)
 
Last edited by a moderator:

raindog308

vpsBoard Premium Member
Moderator
Semi-unrelated trick - in many scripting environments, the slash is arbitrary.

For example, in vi and perl (and probably others) this is valid and works...let's say to replace all instances of http:// with https://


s#http://#https://#g

Makes reading regexes a lot easier and avoids backslashitis.

Not sure about php.
 
Top
amuck-landowner