Results 1 to 6 of 6

Thread: Some Regex Help?

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default Some Regex Help?

    I'm trying to check to make sure a variable equals something like the following:

    (number)(possible number)(possible number).jpg

    So, for example: 1.jpg would work, 20.jpg would work, 101.jpg would work...but 10000.jpg or something.txt wouldn't work.

    Here's my code, which is returning the following error:

    PHP Code:
    if(preg_match('^[1-9][1-9]?[1-9]?\(.jpg)$',$src)){ echo "yeah!";} else{ echo "no!";} 
    Code:
    Warning: preg_match(): No ending delimiter '^' found in /home/content/a/l/e/alexjewell/html/clients/quinn/regexImg.php on line 5
    Any ideas?
    Thanks
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    you forgot the forward slashs ( / )
    PHP Code:
    if( preg_match("/^\d{1,3}(.jpg)$/"$src) ) ? echo "yeah!" : echo "no!"
    \d = digit quantifier
    {1,3} = must be at least 1 digit and cannot be any more then 3 digits

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    And you forgot to escape the dot and misused the ternery conditional operator:
    Code:
    echo preg_match('/^\d{1,3}\.jpg$/', $src) ? 'yeah!' : 'no!';
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Thanks guys - I'm still learning regex.

    Oh, and I have no idea how the if got in there. :/
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Your if was correct, boogyman's wasn't.

    A breakdown of the expression:
    Code:
    '
    A lot of regex characters are also special characters in PHP. Using single-quoted strings means we don't have to worry about escaping them, as well as being slightly more efficient.
    Code:
    /
    This means "start regular expression." Whatever you use here must be escaped within the expression proper, and you can use one of several different characters instead, such as # or ^, if this is easier for your expression. Then the expression says to
    Code:
    ^
    directly after the start of the the string, find
    Code:
    \d
    a digit
    Code:
    {1,3}
    repeated between one and three times, followed by
    Code:
    \.jpg
    the literal string .jpg (the dot has to be escaped since it's a special character that means "any character" in regex) and then
    Code:
    $
    the end of the string.
    Code:
    /
    End regex.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by Twey View Post
    And you forgot to escape the dot and misused the ternery conditional operator:
    Code:
    echo preg_match('/^\d{1,3}\.jpg$/', $src) ? 'yeah!' : 'no!';
    bah I knew something looked off. I probably should start testing my solutions before providing them, would stop alot of these lil errors :|

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
  •