#1
  1. No Profile Picture
    Banned
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Apr 2005
    Posts
    4,982
    Rep Power
    0

    Matching cat2,cat6,cat1,


    I need to match a string that is the word "cat", an integer, and a comma... Like this:

    cat2,cat6,cat1,

    or

    cat10,

    or

    cat7,cat9,cat1,cat,

    I've tried this:

    preg_match("/^cat\d+,*$/", $cats);

    But that's not it...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Nov 2001
    Location
    North West UK
    Posts
    1,147
    Rep Power
    396
    You don't want the beginning an end ^ and $, and probably want to select the values as a group (with brackets).

    preg_match_all("/(cat\d+,)/", $cats, $matches);

    Or
    preg_match_all("/^(cat\d+,)*$/", $cats, $matches);
    if you want to see if the entire string contains nothing but cats
    Last edited by Mark Baker; September 14th, 2009 at 05:11 AM.
    9 out of 10 PHP problems can be resolved by setting
    PHP Code:
    error_reporting(E_ALL);
    ini_set('display_errors'1); 
    php -l <filename> will identify 9 out of the remaining 10 problems
    Remember, the command line is your friend

    Development Projects:
    PHPExcel
    PHPPowerPoint

IMN logo majestic logo threadwatch logo seochat tools logo