0

First of all I would like to apologize for possible mistakes, English is not my native language. The company I work for forces my to install interspire shopping cart on many of our websites and that's okay, I'm pretty familiar with it but one thing I could not understand (I cannot read every single file to find the answer so I am asking if anyone already knows it) is how their global variables(placeholders) work. If a global variable in the php file is named $GLOBALS['sample'], to call it in html file is enough to write this - %%GLOBAL_sample%%. I have some ideas like using function with strreplace which goes to included htmls and replaces the variables with their php content.But how do they make the script edit html files? So does anyone knows how they do it?

1 Answers1

1

I have done something similar in my own code, and the way I do it is along these lines:

ob_start(function($data) {
    $data = str_replace("%KEYWORD%","replacement",$data);
    return $data;
});

I place that at the beginning of my code. Obviously it's more useful the more keywords there are, and you can of course loop through global variables and look for anything matching them, so this code is open to a lot of adaptation. That's just how I'd go about doing something like this.

EDIT: I've just come across a neat method that uses an associative array to do the replacements.

ob_start(function($data) {
    return preg_replace_callback(
        "(%([^% ]+)%)",
        function($m) {
            static $reps = Array(
                "sample" => "replacement",
                "test" => "successful",
                "message" => "Hello"
            ); // define your keywords here
            if( isset($reps[$m[1]])) return $reps[$m[1]];
            return $m[0];
        },
        $data);
});

Sample input:

%message%. This example will test %sample% and hopefully is %test%.

Output:

Hello. This example will test replacement and hopefully is successful.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Thanks for your answer. I was thinking something like that. But I cannot figure out this - okay you make the script which replaces the %%variables%% in some text. But how do you get them from the html files? I mean if you include *.html file in php and it doesn't contain php tags the parser will show everything in plain text. So how do you index the placeholder which you want to replace? – user1163870 Jan 22 '12 at 20:33
  • edit: I think I figure it out - $var = file_get_contents('test.html'); $rep = str_replace("GLOBAL", "something", $var); – user1163870 Jan 22 '12 at 20:43
  • Added a better way of handling replacements. See edit in answer. – Niet the Dark Absol Jan 22 '12 at 21:34