preg_match function for URLs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Latio

    preg_match function for URLs

    Below is a function I've written for validating URLs.

    function isURL ($string, $fieldname)
    {
    if(preg_match("/^[http://www.|www.][\S]+$/", $string))
    {
    return TRUE;
    }
    else
    {
    $this->number_of_erro rs++;
    $this->error_message_ list[]="The " .$fieldname. " is not a valid URL.";
    return FALSE;
    }
    }

    However I keep getting the error message:
    Warning: preg_match() [function.preg-match]: Unknown modifier '/'

    This is the code that I call it:

    if ($testValidate->isURL($testStr ing11,"String 11"))
    {
    echo "The string <b>".$testStrin g11."</bpassed.";
    }
    else
    {
    echo "The string <b>".$testStrin g11."</bfailed.";
    }

    What have I obviously missed?

    Cheers

    Phil




  • Rik

    #2
    Re: preg_match function for URLs

    On Wed, 20 Jun 2007 05:47:27 +0200, Phil Latio
    <phil.latio@f-in-stupid.co.ukwro te:
    Below is a function I've written for validating URLs.
    if(preg_match("/^[http://www.|www.][\S]+$/", $string))
    --------------------------^
    However I keep getting the error message:
    Warning: preg_match() [function.preg-match]: Unknown modifier '/'
    >
    What have I obviously missed?
    That if you start a regex with '/', any '/' will end it, and the rest od
    the string has to be interpreted as modifiers. You can use anything as a
    modifier, so just use another character.


    --
    Rik Wasmus

    Comment

    • Phil Latio

      #3
      Re: preg_match function for URLs


      "Rik" <luiheidsgoeroe @hotmail.comwro te in message
      news:op.tt7ot5p 2qnv3q9@metalli um...
      On Wed, 20 Jun 2007 05:47:27 +0200, Phil Latio
      <phil.latio@f-in-stupid.co.ukwro te:
      >
      >Below is a function I've written for validating URLs.
      >if(preg_match( "/^[http://www.|www.][\S]+$/", $string))
      --------------------------^
      >
      >However I keep getting the error message:
      >Warning: preg_match() [function.preg-match]: Unknown modifier '/'
      >>
      >What have I obviously missed?
      >
      That if you start a regex with '/', any '/' will end it, and the rest od
      the string has to be interpreted as modifiers. You can use anything as a
      modifier, so just use another character.
      >
      >
      --
      Rik Wasmus
      Thanks for that. I am not really familiar regular expressions yet but well
      written ones seem quite useful.

      Cheers

      Phil


      Comment

      • Michael Fesser

        #4
        Re: preg_match function for URLs

        ..oO(Phil Latio)
        >Thanks for that. I am not really familiar regular expressions yet but well
        >written ones seem quite useful.
        There's another error in your pattern:

        /^[http://www.|www.][\S]+$/

        The first part creates a character class, which simply means to match
        one of the characters [htp:/w.|]. Use parentheses instead to create a
        subpattern, use another delimiter and maybe the /i modifier:

        #^(http://www\.|www\.)[\S]+$#i

        or try

        #^(https?://)?www\.[\S]+$#i

        But validating an entire URL is a bit more complex ...

        Micha

        Comment

        Working...