Page 1 of 1
replace matches with string content
Posted: Fri Dec 04, 2009 1:09 pm
by ProTec
I am trying to find a preg_replace expression to scan a file for "placeholders" they all have the following format:
[[placeholder]]
[[link_abroad]]
[[you_get_the_picture]]
I want them to be replaced by the contents of the matching string, that has been defined earlier in the document. For example in this case:
$placeholder = "this is string 1";
$link_abroad = "this is definitely two";
$you_get_the_picture = "When you see three, you know it works huh?";
$tobetested=" the expression should say: [[placeholder]] and here it would state <b> [[link_abroad]]</b><br />However......[[you_get_the_picture]].... while this results in [[empty]]
$tobetested=preg_replace("/\[\[(somethinghere)]]/s", @$$1, $tobetested);
print $tobetested;
The output should be:
the expression should say: this is string 1and here it would state this is definitely 2
However......When you see three, you know it works huh?";.... while this results in
I wouldn't be here if I am not beat. I have had a lot of results, but nothing remotely represents what I want.
Someone?
Re: replace matches with string content
Posted: Fri Dec 04, 2009 2:50 pm
by AbraCadaver
Several ways without a regex. Here is how I would probably do it:
Code: Select all
$search = array('placeholder', 'link_abroad', 'you_get_the_picture');
$replace = array('this is string 1', 'this is definitely two', 'When you see three, you know it works huh?');
$tobetested=" the expression should say: [[placeholder]] and here it would state <b> [[link_abroad]]</b><br />However......[[you_get_the_picture]].... while this results in [[empty]]";
$tobetested = str_replace("[[$search]]", $replace, $tobetested);
-Shawn
Re: replace matches with string content
Posted: Sat Dec 05, 2009 6:57 am
by ProTec
No. I don't need that solution. I need a regex version, because not all the placeholders are known yet.
Someone?
Re: replace matches with string content
Posted: Sat Dec 05, 2009 1:01 pm
by AbraCadaver
AFAIK you're not going to be able to do it in one regex replace because of the variables, but this works:
Code: Select all
$placeholder = "this is string 1";
$link_abroad = "this is definitely two";
$you_get_the_picture = "When you see three, you know it works huh?";
$tobetested=" the expression should say: [[placeholder]] and here it would state <b> [[link_abroad]]</b><br />However......[[you_get_the_picture]].... while this results in [[empty]]";
preg_match_all('/\[\[(.*?)\]\]/s', $tobetested, $matches);
$search = $matches[0];
$variables = $matches[1];
$replace = array();
foreach($variables as $variable) {
if(isset($$variable)) {
$replace[] = $$variable;
} else {
$replace[] = '';
}
}
$tobetested = str_replace($search, $replace, $tobetested);
HTH
-Shawn
Re: replace matches with string content
Posted: Sun Dec 06, 2009 8:27 pm
by ridgerunner
The solution is to use the 'e' pattern modifier to cause preg_replace() to evaluate the replacement text as PHP code, and then simply replace '[[token]]' with '$token' like so:
Code: Select all
<?php
$placeholder = "this is string 1";
$link_abroad = "this is definitely two";
$you_get_the_picture = "When you see three, you know it works huh?";
$tobetested=" the expression should say: [[placeholder]] and here it would state <b> [[link_abroad]]</b><br />However......[[you_get_the_picture]].... while this results in [[empty]]";
$tobetested=preg_replace('/\[\[(\w+)\]\]/e', '$$1', $tobetested);
print $tobetested;
?>
Note, however, that the test data you supplied has a placeholder [[empty]] for which no corresponding variable was defined. This results in a PHP warning message:
Notice: Undefined variable: empty in test.php(8) : regexp code on line 1 Otherwise this regex does exactly what you are looking for.
Hope this helps!

Re: replace matches with string content
Posted: Sun Dec 06, 2009 11:09 pm
by AbraCadaver
ridgerunner wrote:The solution is to use the 'e' pattern modifier to cause preg_replace() to evaluate the replacement text as PHP code, and then simply replace '[[token]]' with '$token' like so:
Nice one. I didn't know about the 'e'.
On a side, check this one:
viewtopic.php?f=1&t=109840&p=581570
I know he posted a lot of code, but you should be able to help if I'm off.
-Shawn
Re: replace matches with string content
Posted: Mon Dec 07, 2009 9:36 am
by ProTec
ridgerunner wrote:The solution is to use the 'e' pattern modifier to cause preg_replace() to evaluate the replacement text as PHP code, and then simply replace '[[token]]' with '$token' like so:
Code: Select all
<?php
$placeholder = "this is string 1";
$link_abroad = "this is definitely two";
$you_get_the_picture = "When you see three, you know it works huh?";
$tobetested=" the expression should say: [[placeholder]] and here it would state <b> [[link_abroad]]</b><br />However......[[you_get_the_picture]].... while this results in [[empty]]";
$tobetested=preg_replace('/\[\[(\w+)\]\]/e', '$$1', $tobetested);
print $tobetested;
?>
Note, however, that the test data you supplied has a placeholder [[empty]] for which no corresponding variable was defined. This results in a PHP warning message:
Notice: Undefined variable: empty in test.php(8) : regexp code on line 1 Otherwise this regex does exactly what you are looking for.
Hope this helps!

Absolutely!!!! I knew it. I just couldn't remember how I did it before. This is almost exactly the one liner I was talking about.
@ will take care of the warning! GREAT!!!
$tobetested=preg_replace('/\[\[(\w+)\]\]/e', '
@$$1', $tobetested);
Thanks all of you!!