views:

22

answers:

1

Sorry for this basic question, but I've been looking over all the info about preg_replace I can find and I still can't figure this out.. I have a large string, like this, for example:

  $string= '# tjs { fassdaf } #fsk { fssf} # fskff { casf }';

And when I do this, it replaces the entire pattern, not just the part in ( ) as I expect it to do.. I am wondering how I can just replace the part in ( ).. thanks

  $pattern= '/#.*tjs.*\{.*(.*)\}/imsU';
  $replacement= "test";
  $return_string = preg_replace ($string, $pattern, $replacement );

expected replaced string:

'# tjs {test} #fsk { fssf} # fskff { casf }';
+1  A: 
$pattern= '/(#\s*tjs\s*\{\s*)(.*?)(\s*\})/imsU';
$replacement= "test";
$return_string = preg_replace($pattern,'$1'.$replacement.'$3',$string);
stillstanding
Thanks, now I understand, I saw the mention of $1 and $3 in an example on the php preg_replace page but they used some strange syntax with { so I think that threw me off
Rick