regex - PHP - Preg_match_all optional match -
i'm having problems matching the[*] there , not. have suggestions?
$name = 'hello $this->row[today1][] dfh fgh df $this->row[test1] ,how $this->row[test2][] $this->row[today2][*] monday'; echo $name."\n"; preg_match_all( '/\$this->row[.*?][*]/', $name, $match ); var_dump( $match );
output: hello $this->row[test] ,how $this->row[test2] $this->row[today][*] monday
array ( 0 => array ( 0 => '$this->row[today1][*]', 1 => '$this->row[test1] ,how $this->row[test2][*]', 2 => '$this->row[today2][*]', ), )
now [0][1] match takes on because matching until next '[]' instead of ending @ '$this->row[test]' . i'm guessing [*]/ adds wildcard. somehow need check if next character [ before matching []. anyone?
thanks
[
, ]
, *
special meta characters in regex , need escape them. need make last []
optional per question.
following these suggestions following should work:
$name = 'hello $this->row[today1][] dfh fgh df $this->row[test1] ,how $this->row[test2][] $this->row[today2][*] monday'; echo $name."\n"; preg_match_all( '/\$this->row\[.*?\](?:\[.*?\])?/', $name, $match ); var_dump( $match );
output:
array(1) { [0]=> array(4) { [0]=> string(20) "$this->row[today1][]" [1]=> string(17) "$this->row[test1]" [2]=> string(19) "$this->row[test2][]" [3]=> string(21) "$this->row[today2][*]" } }
Comments
Post a Comment