Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on small template class. I need help in converting {$variable} written in template file to be converted to

Like :

<html>
   <body>
      <p> Hey Welcome {$username} </p> 
   </body>
</html>

to be converted to

    <html>
   <body>
      <p> Hey Welcome <?php echo $username ?> </p> 
   </body>
</html>

Just like variable username. there can be any variable with any length. I just want to convert it to php echo statment.

I think it is possible with preg_replace() but don't know how.

share|improve this question
1  
You can read how here:php.net/manual/en/function.preg-replace.php –  Casimir et Hippolyte Jun 14 '13 at 12:52
    
Why don't you try simple: $html_template = str_replace('{$username}', $username, $html_template); –  Stanislav Terleckiy Jun 14 '13 at 12:56
    
Why preg_replace() and not str_replace()? Why replace it with PHP code instead of the real value? –  str Jun 14 '13 at 12:57
add comment

3 Answers

up vote 0 down vote accepted

How's this?

$string = 'Hello {$username}, how are you?';
$new_string = preg_replace('/\{(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/', '<?php echo \\1; ?>', $string);
echo $new_string;

Which gives this:

Hello <?php echo $username; ?>, how are you?

I borrowed that expression from the php manual..

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

So in theory it should match any valid variable.

share|improve this answer
    
Thank you ! This is exactly same what i need. –  Poush Jun 14 '13 at 15:10
    
Will you tell me any tutorial for regular expression –  Poush Jun 14 '13 at 15:15
    
@Poush One good resource that springs to mind is regular-expressions –  Dale Jun 14 '13 at 18:42
add comment
preg_replace('/\{\$username\}/', '<?php echo $username; ?>', $text);

or in general:

preg_replace('/\{\$([^\}]+)\}/', '<?php echo $$1; ?>', $text);
share|improve this answer
    
Thank you! Will you tell me any tutorial for regular expression ? –  Poush Jun 14 '13 at 15:14
add comment

For example: you have app folder structure:

  • app/view/index.template
  • app/controller/index.php
  • app/index.php

Where "app" folder is webroot

So, file "app/view/index.template" contains:

<html>
   <body>
      <p> Hey Welcome {$username} </p> 
   </body>
</html>

And "app/controller/index.php" contains next:

<?php
    $username = 'My Hero';
    $content = file_get_contents(__DIR__ . '../view/index.template');
    if ($content) {
        echo str_replace('{$username}', $username, $content);
    } else { echo 'Sorry, file not found...';}

And "app/index.php" contains next:

<?php
    include __DIR__ . '/controller/index.php';

Something like that...

share|improve this answer
    
Your idea in nice but my script has not limited variable. I have to write str_replace() for each statement which is not even set(known) when making controller. Controller will get list of variables by another script/page during executing. –  Poush Jun 14 '13 at 15:08
    
It is possible to use it in that way: str_replace(array('tmp1', 'tmp2', ... 'tmp_n'), array($var1, $var2, ... $var_n), $content); –  Stanislav Terleckiy Jun 14 '13 at 15:47
    
Note, preg functions eats huge count resources, where it possible we aren't need use them. –  Stanislav Terleckiy Jun 14 '13 at 15:51
    
And you can organize you template handler function with loop like: $vars = array('tmp_1'=>$var1, 'tmp_2'=>$var2, ... 'tmp_n'=>$var_n); echo render($content, $vars); function render($content, $vars) { foreach ($vars as $tmp => $var) { str_replace("{$$tmp}", $var, $content); } return $content; } –  Stanislav Terleckiy Jun 14 '13 at 15:54
    
Wow! Thanks for this great idea. –  Poush Jun 14 '13 at 16:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.