53329 Posted August 13, 2008 Share Posted August 13, 2008 class testclass { function begin() { $TEMPLATE= "<span> {NAME} {WEBSITE} {PROFILE} {OPTIONS} </span>"; $text = preg_replace('/\{(\w+)\}/e', "$this->components['$1']", $TEMPLATE); echo $text; } } $test=new testclass(); $test->components=array('NAME'=>'my name','WEBSITE'=>'my website','PROFILE'=>789,'OPTIONS'=>123); $test->begin(); Error is here $text = preg_replace('/\{(\w+)\}/e', "$this->components['$1']", $TEMPLATE); without escaping the [] I get the error: Parse error: syntax error, unexpected '[', expecting '(' When I change the line to $text = preg_replace('/\{(\w+)\}/e', "$this->components\['$1'\]", $TEMPLATE); I get the error: Unexpected character in input: '\' (ASCII=92) state=1 Is there a way to escape the brackets? Quote Link to comment Share on other sites More sharing options...
effigy Posted August 14, 2008 Share Posted August 14, 2008 Your replacement is being interpreted before the evaluation process--swap the quoting: '$this->components["$1"]'. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.