KevinM1 Posted October 10, 2007 Share Posted October 10, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72622-solved-validating-a-url/ Share on other sites More sharing options...
effigy Posted October 10, 2007 Share Posted October 10, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/72622-solved-validating-a-url/#findComment-366182 Share on other sites More sharing options...
KevinM1 Posted October 10, 2007 Author Share Posted October 10, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/72622-solved-validating-a-url/#findComment-366185 Share on other sites More sharing options...
effigy Posted October 10, 2007 Share Posted October 10, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72622-solved-validating-a-url/#findComment-366186 Share on other sites More sharing options...
KevinM1 Posted October 10, 2007 Author Share Posted October 10, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72622-solved-validating-a-url/#findComment-366190 Share on other sites More sharing options...
effigy Posted October 10, 2007 Share Posted October 10, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/72622-solved-validating-a-url/#findComment-366196 Share on other sites More sharing options...
KevinM1 Posted October 10, 2007 Author Share Posted October 10, 2007 Thanks a bunch! Quote Link to comment https://forums.phpfreaks.com/topic/72622-solved-validating-a-url/#findComment-366201 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.