Page 1 of 1

is there regex for this?

Posted: Thu Jan 12, 2006 4:36 pm
by devosc
Hi

Code: Select all

$this->aVar['Template']->get('MyTest') == 'test' ? $Template.get('MyTest') : $Template.get('MyTest')
I would like to match instances where a sequence of chars start with $ but not $this.

Its to go in a preg_replace, but I've been trying to make it work with preg_match_all while trying to figure it out:

Code: Select all

if (preg_match_all('@\$([^\s]*)@S', $aAttr[$sKey], $aMatches)) {
          var_dump($aAttr[$sKey], $aMatches);
        }

Code: Select all

array
  0 => 
    array
      0 => '$this->aVar['Template']->get('MyTest')'
      1 => '$Template.get('MyTest')'
      2 => '$Template.get('MyTest')'
  1 => 
    array
      0 => 'this->aVar['Template']->get('MyTest')'
      1 => 'Template.get('MyTest')'
      2 => 'Template.get('MyTest')'
I just seem to, or rather, I don't currently know, how to get it to not match $this yet match the others

Posted: Thu Jan 12, 2006 8:13 pm
by feyd

Code: Select all

[feyd@home]> php -r "$a = '$this->aVar[\'Template\']->get(\'MyTest\') == \'test\' ? $Template.get(\'MyTest\') : $Template.get(\'MyTest\')'; preg_match_all('#\$(?!this)[A-z_][^\s]*#i',$a,$m); var_export($m);"
array (
  0 =>
  array (
    0 => '$Template.get(\'MyTest\')',
    1 => '$Template.get(\'MyTest\')',
  ),
)

Posted: Thu Jan 12, 2006 8:57 pm
by devosc
Hi feyd,

Thanks alot, just had to tweak it a little to drop the $; works like a dream. Many Thanks.

G.