SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple Regex Problem in PHP

    Hi there,

    I'm a regex noob trying to write a regular expression to check the protocol at the start of a valid URL.

    I've got this-

    Code:
    $test = ereg( '^(https?|ftp|file):\/\/$', $url );

    If I pass any valid url to it, $test returns false when I would expect it to return true. Any ideas where I've gone wrong?

  2. #2
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind, figured it out. Was missing the .+

    $test = ereg( '^(https?|ftp|file):\/\/.+$', $url );

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $reg '~^(?:https?|ftps?|file|[-\w+.]+)://~i'
    var_dumppreg_match$reg$url ) ? true false ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •