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? Link to comment https://forums.phpfreaks.com/topic/119555-solved-replace-tag-with-array-value/ 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"]'. Link to comment https://forums.phpfreaks.com/topic/119555-solved-replace-tag-with-array-value/#findComment-616517 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.