Jump to content

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.