Forum Moderators: coopster

Message Too Old, No Replies

Find replace tokens

Without knowing token name

         

Philosopher

6:35 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm making some modifications to a script that reads a template file and replaces tokens in the form of %%WORD%% with a call to a file. Below is the original code to accomplish this

$dir = opendir("include");
while ($filename = readdir($dir)){
if ($filename!= '.' && $filename!= '..' ) {
$template = str_replace("%%".$filename."%%","<? @include(\"include/$filename\");?>",$template);
$template = str_replace("%%".strtoupper($filename)."%%","<? @include(\"include/$filename\");?>",$template);
$template = str_replace("%%".strtolower($filename)."%%","<? @include(\"include/$filename\");?>",$template);

}
}

As you can see, the script would read the directory where the various includes were found and use their names along with a while loop to find the tokens and parse them properly.

For upgrade purposes, I'd like to parse the tokens through a regex instead of being limited to files found in the include directory.

Unfortunatley regex, even simple ones aren't my strong suit. I know it should be fairly simple to use a regex pattern to grab the text between the %% and the %%, assign it to a variable, then add it to an include line, but I'm having difficulties today.

Any help would be appreciated.

Thanks!

coopster

8:55 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Are you just wondering what a regex might look like to grab the values between double percentage signs might be?
$pattern = "/%%([^%]+)%%/";

This says to find and capture one or more non-percent-sign characters found between sets of double percent sign characters. You could then use that in a preg_replace() function or perhaps a preg_replace_callback() function.