tags:

views:

78

answers:

7

hey guys

i used bellow code to search and find if http is includes in $url address user enters

if (!preg_match("/http:///", $user_website) 

but i got this error

Warning: preg_match() [function.preg-match]: Unknown modifier '/' in

i know its becuase of // of http but how work arround this !?

+1  A: 

Escape / characters with \ characters.

David Dorward
A: 

You need to escape literal characters. Place a back-slash before your forward slashes.

http:// becomes http:\/\/

Jonathan Sampson
A: 
if (!preg_match("/http:\/\//", $user_website) 
Matt Williamson
+2  A: 

Instead of having to escape every / in URL regexes it's often useful to use preg_* alternative characters to mark the start/end of the pattern.

if (!preg_match("#http://#", $user_website)
Kendall Hopkins
+2  A: 

You can escape the slashes like the other answers mention, or alternatively you can use different delimiters, preferably characters you won't use in your regex:

preg_match('~http://~', ...)
preg_match('!http://!', ...)

And you don't really need regex for this. String matching should be enough:

if (strpos($user_website, 'http://') !== false) {
    // do something
}

See: strpos()

NullUserException
+1 for mention strpos
Benjamin Cremer
would've been a +1 if there already wasn't a parse_url function
shylent
@shy It doesn't seem like the OP wants to parse the URL
NullUserException
well, it does seem so to me :)
shylent
+2  A: 

The delimiter you are using / is found in the pattern as well. In such cases you can either escape the delimiter in the pattern:

if (!preg_match("/http:\/\//", $user_website) 

or you can choose a different delimiter. This will keep the pattern clean and short:

if (!preg_match("#http://#", $user_website) 
codaddict
+2  A: 

Surely you must do

$parts = parse_url($my_url);

$parts['scheme'] will then contain the url scheme (might be 'http').

shylent
+1 for mentioning the right tool for the job. Don't use a regex to parse a URL when there's already a perfectly good built-in!Also, due to a missing `^`, the OP's regex would match `https://example.com/a/b/c/http://x/y/z` which is a valid-enough URL (I recently encountered a site that served content from a URL like the above)
Frank Farmer