SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast HARS's Avatar
    Join Date
    Jun 2001
    Location
    Stockholm, Sweden
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match explanation?

    hey all, this should be fairly easy to solve as i'm guessing this is a pretty basic question

    i'm trying to check if a variable passed from a form is a valid address, containing letters, . or :
    but php.net/preg_match doesn't help me much, somewhere between all the /\/// i get lost...

    what i've scraped together is:
    PHP Code:
     //letters, . and :
      
    function isAddress($element) {
        return !
    preg_match ("/something here/"$element);
      } 
    it would be nice if you could help me out here, ir send me off to some tutorial i haven't found yet... cheers!

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2004
    Location
    Madison, WI
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm not great with regexes, but if you want a tutorial there's a great one over at http://www.regular-expressions.info

  3. #3
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // match letters commas periods and colons
    function isAddress($element)
    { return !
    preg_match ("/[a-zA-Z,.: ]*/"$element); }

    // match letters commas periods and colons AND numbers
    function isAddress($element)
    { return !
    preg_match ("/[0-9a-zA-Z,.: ]*/"$element); } 
    for a library of common regexs, also see: http://www.regexlib.com

  4. #4
    SitePoint Enthusiast HARS's Avatar
    Join Date
    Jun 2001
    Location
    Stockholm, Sweden
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, so you just add what you want to find between [ and ], nice. but what's with the * ? and i've seen ^ being used, but don't really know why.
    Last edited by HARS; Nov 12, 2004 at 07:57.

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > valid address, containing letters, . or :

    your regexp is ~^[A-Za-z.:]+$~

    where
    ~ is starting delimiter
    ^ means "beginning of search string"
    [A-Za-z.:] is "uppercase letter or lowercase letter or dot or colon"
    + means "at least one"
    $ is "end of search string"
    ~ is ending delimiter.

    So,

    PHP Code:
    function isAddress($str) {
       return 
    preg_match("~^[A-Za-z.:]+$~"$str);

    note you don't need "!" (which means "not") before preg_match.

  6. #6
    SitePoint Enthusiast HARS's Avatar
    Join Date
    Jun 2001
    Location
    Stockholm, Sweden
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    great! that is exactly the explanation i was looking for thanks!
    i'm using ! 'cause i want it to return false if an element isn't found...


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
  •