replace matches with string content

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
ProTec
Forum Newbie
Posts: 3
Joined: Fri Dec 04, 2009 12:54 pm

replace matches with string content

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: replace matches with string content

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
ProTec
Forum Newbie
Posts: 3
Joined: Fri Dec 04, 2009 12:54 pm

Re: replace matches with string content

Post by ProTec »

No. I don't need that solution. I need a regex version, because not all the placeholders are known yet.

Someone?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: replace matches with string content

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: replace matches with string content

Post 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! :)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: replace matches with string content

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
ProTec
Forum Newbie
Posts: 3
Joined: Fri Dec 04, 2009 12:54 pm

Re: replace matches with string content

Post 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!!
Post Reply