Jump to content

[SOLVED] replace tag with array value


53329

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.