#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Preg_match_all optional match


    I'm having problems matching the[*] which is sometimes there and sometimes not. Anyone have suggestions?

    PHP Code:
    $name 'hello $this->row[test] ,how good $this->row[test2] is $this->row[today][*] is monday';
    echo 
    $name."\n";
    preg_match_all'/(\$this->row(\[.*?\])|[*])/'$name$match );
    var_dump$match ); 
    output:
    hello $this->row[test] ,how good $this->row[test2] is $this->row[today][*] is monday
    array(3) {
    [0]=>
    array(4) {
    [0]=>
    string(16) "$this->row[test]"
    [1]=>
    string(17) "$this->row[test2]"
    [2]=>
    string(17) "$this->row[today]"
    [3]=>
    string(1) "*"
    }
    [1]=>
    array(4) {
    [0]=>
    string(16) "$this->row[test]"
    [1]=>
    string(17) "$this->row[test2]"
    [2]=>
    string(17) "$this->row[today]"
    [3]=>
    string(1) "*"
    }
    [2]=>
    array(4) {
    [0]=>
    string(6) "[test]"
    [1]=>
    string(7) "[test2]"
    [2]=>
    string(7) "[today]"
    [3]=>
    string(0) ""
    }
    }


    But I would like it to output "$this->row[today][*]" instead of "$this->row[today]"

    Thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    your regex doesn't make a lot of sense. The pipe character | is an "or", so you've specified two alternative patterns. You also seem to have forgotten to escape the brackets in the [*] expression. Without escaping, this expression is a character class matching a single * character.

    All in all, what your regex does is look for $this->row[...] expressions or any * character.

    I also don't understand your use of parenthesis (which capture submatches).

    OK, so you want a $this->row[...] expression followed by an optional [*], right? Then you want the ? quantifier, which denotes an optional expression:

    PHP Code:
    '/\\$this->row\\[[^\\]]*\\](?:\\[\\*\\])?/' 
    Which means: $this->row[...] optionally followed by an [*].
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0
    Thanks, your solution was very close to what I needed.
    Altered code:
    /\$this->row\[.*?\](?:\[\*\])?/

    output:
    hello $this->row[today][*] dfh fgh df $this->row[test] ,how good $this->row[test2][*] is $this->row[today][*] is monday
    array(1) {
    [0]=>
    array(4) {
    [0]=>
    string(20) "$this->row[today][*]"
    [1]=>
    string(16) "$this->row[test]"
    [2]=>
    string(20) "$this->row[test2][*]"
    [3]=>
    string(20) "$this->row[today][*]"
    }
    }

IMN logo majestic logo threadwatch logo seochat tools logo