Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

URL Validation

Last post 08-21-2012, 6:07 PM by g1smd. 2 replies.
Sort Posts: Previous Next
  •  05-27-2007, 8:22 PM 29959

    URL Validation

    I borrowed one of the url validation expressions on the site, I tested it on site as well and it worked as expected.

    However, when I copied it into my code, I added the ("/ prefix in this manner:

    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\&%_\./-~-]*)?/", $url))

    In doing so i get this error

    Warning: preg_match() [function.preg-match]: Unknown modifier '/' in /home etc etc on line 154

    If I remove the first /, I then get the error telling me I have an unknown + in the code.  I am sure removing that will negate the codes operation.   I am sure the first / is a delimiter and

    not a modifier, but I cant see anything wrong with the code i borrowed.  I did review the code previous to this code and can find no mistakes.

    Any suggestions for a beginner?  Thanks and have a great weekend

     

  •  07-03-2007, 8:06 PM 32423 in reply to 29959

    Re: URL Validation

    I hope that this response is not too late for your needs, but I've only just seen this.

    I have not used php myself, but I do remember something from Friedl's book about the delimiters that go around the regex pattern. My guess is that you will need to escape the '/' characters that you have in the expression itself. My understanding is that php will use the first character it finds as the delimiter, and will then scan the rest of the string for the matching one which it will interpret as the end of the pattern. You have used as delimiter character that also appears in your pattern.

    There are two options: use a different delimiter character (I think '#' might do in your case) or escape the '/'s within the pattern.

    Also, don't you need the matching delimiter at the end of the pattern?

    Susan 

  •  08-21-2012, 6:07 PM 86247 in reply to 29959

    Re: URL Validation

    You have a RegEx pattern something like:    "/ <somestuff> /"

     If the <somestuff> part itself contains slashes, you should use a different delimiter at the ends of the pattern:    "# <somestuff> #"  is probably suitable.

View as RSS news feed in XML