Jump to content

[SOLVED] Validating a URL


KevinM1

Recommended Posts

I'm getting a parse error using a regular expression I got from regexlib.net.  The specifics are:

 

Parse error:

Warning: preg_match() [function.preg-match]: Unknown modifier '\' in /home/nights/www/www/rextrader/admin/config.php on line 184

 

The line in question:

<?php

if(preg_match("/^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp):\/\/)|(www\.))*(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\/[a-zA-Z0-9\&%_\./\-~\-]*)?$/", $_POST['siteAddr'])){

?>

 

As you can see, the regex is pretty involved, more complicated than a newbie like myself can debug, especially with all of the '\' characters within it.  I'm hoping that someone else's trained eye can catch my error before I'm forced to eliminate each '\' one-by-one.

Link to comment
https://forums.phpfreaks.com/topic/72622-solved-validating-a-url/
Share on other sites

Always use /x for expressions like this and change the delimiters to avoid excess backslashing. This could still be improved upon in terms of optimization and standards, but it should be error free.

 

!

^ ### BOL

### Match an optional www or protocol.

(?:

www\.

|

(?:file|gopher|news|nntp|telnet|https?|ftps?|sftp)://

)?

### Match a domain or an IP.

(?:

(?:

[-a-zA-Z0-9._]+\.[a-zA-Z]{2,6}

)

|

(?:

(?:\d{1,3}\.){3}\d{1,3}

)

)

### Match the rest, optional.

(?:

/[-a-zA-Z0-9&%_./~]*

)?

$

!x

 

Always use /x for expressions like this and change the delimiters to avoid excess backslashing. This could still be improved upon in terms of optimization and standards, but it should be error free.

 

!

^ ### BOL

### Match an optional www or protocol.

(?:

www\.

|

(?:file|gopher|news|nntp|telnet|https?|ftps?|sftp)://

)?

### Match a domain or an IP.

(?:

(?:

[-a-zA-Z0-9._]+\.[a-zA-Z]{2,6}

)

|

(?:

(?:\d{1,3}\.){3}\d{1,3}

)

)

### Match the rest, optional.

(?:

/[-a-zA-Z0-9&%_./~]*

)?

$

!x

 

 

A bit confused here...is /x more or less like regex's version of a heredoc?  Could I place the above in a variable for preg_match's first argument?

x (PCRE_EXTENDED)

If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include comments inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern.

 

Yes.

x (PCRE_EXTENDED)

If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include comments inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern.

 

Yes.

 

Hmm..I can't see how to put it into a variable.  Mind giving me the link?  I'm looking at php.net right now, but haven't found the bit about x yet.

PCRE Pattern Modifiers

 

<pre>
<?php
$regex = '!
	^ ### BOL
	### Match an optional www or protocol.
	(?:
		www\.
		|
		(?:file|gopher|news|nntp|telnet|https?|ftps?|sftp)://
	)?
	### Match a domain or an IP.
	(?:
		(?:
			[-a-zA-Z0-9._]+\.[a-zA-Z]{2,6}
		)
		|
		(?:
			(?:\d{1,3}\.){3}\d{1,3}
		)
	)
	### Match the rest, optional.
	(?:
		/[-a-zA-Z0-9&%_./~]*
	)?
	$
!x';
echo preg_match($regex , 'http://www.phpfreaks.com') ? 'Matched' : 'No Match';
?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.