0

I am very beginner in programmer, I am sorry if my question is crazy thing, but I am being crazy with it :D

I want to create an template for my php file (or replace my string with my php file):

my_string.html

{header}
Some String {foo} Some String {bar} Some String 
{footer}

my_file.php

<div class="header"><img src="logo.png" /></div>
<?php if(isset($foo)) { echo "FOO"; } ?>
<?php if(isset($bar)) { echo "BAR"; } ?>
<div class="footer">mywebsite.com</div>

Can I do something like this?

<?php $find = array("{header}", "{foo}", "{bar}", "{footer}"); ?>   
<?php $replace = array(
"<div class=\"header\"><img src=\"logo.png"\ /></div>",     
"<?php if(isset(\$foo)) { echo \"FOO\"; } ?>",
"<?php if(isset(\$bar)) { echo \"BAR\"; } ?>",
"<div class=\"footer\">mywebsite.com</div>"); ?>

<?php echo str_replace($find, $replace, implode("<br>", file("my_string.html"))); ?>

Results from my_file.php :

<div class="header"><img src="logo.png" /></div>
Some String FOO Some String BAR Some String 
<div class="footer">mywebsite.com</div>

Does anyone have a solution for my problem?

6
  • 1
    Are you trying to create your own template system? The question isn't very clear. Commented Feb 4, 2014 at 0:50
  • @nus Yes, exactly.. I want to create an template for my_file.php Commented Feb 4, 2014 at 1:09
  • actually after trying another 20 captcha's, I'm positive that they don't even look at whether the answer is correct. I surely didn't get all the captcha's right, because they are absolutely unreadable, but I surely got some right. I'm done with trying to help people on stackexchange. bye Commented Feb 4, 2014 at 2:09
  • Hi @nus thanks for your answer, finally I resolved it.. I split my php file into some part file and join it again into a file.. Commented Feb 4, 2014 at 14:37
  • Hi @AjieKurniyawan , welcome to Stack Overflow. Here we prefer to keep questions and answers separated. Thus I separated your answer in a wiki answer (I won't get reputation from it) so feel free to accept it. The best thing to do is you to properly answer your question in the form below and accept your own answer, so that other see the question is answered already. Commented Feb 4, 2014 at 15:08

3 Answers 3

1

I copied/pasted the OP answer here in a community wiki since it was appended to the question.

I split my_file.php file to some part and then rejoin it again

header.php

<div class="header"><img src="logo.png" /></div>

foo.php

<?php if(isset($foo)) { echo "FOO"; } ?>

bar.php

<?php if(isset($bar)) { echo "BAR"; } ?>

footer.php

<div class="footer">mywebsite.com</div>

// Var

$header = implode("", file("header.php"));
$foo = implode("", file("foo.php")); 
$bar = implode("", file("bar.php")); 
$footer = implode("", file("footer.php")); 

// New template
$new_template = "new_template.php";

$find = array ("{header}",  "{foo}", "{bar}", "{footer}");
$replace = array ($header, $foo, $bar, $footer);

$contents = str_replace($find, $replace, implode("<br>", file("my_string.html")));
file_put_contents($new_template, $contents);

// Results
include $new_template;
Sign up to request clarification or add additional context in comments.

Comments

0
<?
/**
 * Replace every pattern from the array that match in the subject with 
 * the given value
 * 
 * @$subject The string to search and replace.
 * @$replace_with Array with key Value like
 *      $replace_with = array('{pattern}' => 'Value', 'Foo' => 'BaR');
*/
function template_replace($subject, $replace_with)
{
    // checking
    if(null == $subject) return null;
    if( !is_array($replace_with) || count($replace_with) == 0)
    {
        return $subject;   
    }

    // iterate over the pattern <=> replacement
    foreach($replace_with as $pattern => $replacement)
    {

        $pattern = preg_quote($pattern);

        // Replace all matches to pattern
        $subject = preg_replace(
            "/$pattern/",
            $replacement,
            $subject
        );
    }
    return $subject;
} 

// -----------------


$template = "{header}
Some String {foo} Some String {bar} Some String 
{footer}";

$pattern = array(
    '{header}' => '<div class="header"><img src="logo.png" /></div>',
    '{foo}' => 'Foo',
    '{bar}' => 'Bar',
    '{footer}' => '<div class="footer">mywebsite.com</div>'
    );

$result = template_replace($template, $pattern);

echo $result;

/*
<div class="header"><img src="logo.png" /></div>
Some String Foo Some String Bar Some String 
<div class="footer">mywebsite.com</div>
*/

3 Comments

This php code <?php if(isset($foo)) { echo "FOO"; } ?> and <?php if(isset($bar)) { echo "BAR"; } ?> are just some example, my php code is more complex.. maybe it won't work with array like this: $pattern = array( '{foo}' => 'Foo', '{bar}' => 'Bar', ... );
If your pieces of code are long, put them in files and use file_get_contents to get them in the array or look at an existing template library I would say (there's plenty, why invent the hot water?).
@Wuiso, there is an error in your code. The patterns aren't valid regular expressions because { and } need to be escaped. You can use preg_quote to do it automatically.
0

If I get it right, you are trying make your own template system. I would use regular expressions:

$html         = file_get_contents( 'my_string.html' );

$replacements = array
(
    array( '/{header}/', 'your html code' )
  , // ... the next one
  , // ... and so on
);


foreach( $replacements as $tag )

   $html = preg_replace( preg_quote( $tag[ 0 ] ), $tag[ 1 ], $html );


echo $html;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.