0

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.

3
  • 1
    You can read how here:php.net/manual/en/function.preg-replace.php Commented Jun 14, 2013 at 12:52
  • Why don't you try simple: $html_template = str_replace('{$username}', $username, $html_template); Commented Jun 14, 2013 at 12:56
  • Why preg_replace() and not str_replace()? Why replace it with PHP code instead of the real value? Commented Jun 14, 2013 at 12:57

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Will you tell me any tutorial for regular expression
@Poush One good resource that springs to mind is regular-expressions
0
preg_replace('/\{\$username\}/', '<?php echo $username; ?>', $text);

or in general:

preg_replace('/\{\$([^\}]+)\}/', '<?php echo $$1; ?>', $text);

1 Comment

Thank you! Will you tell me any tutorial for regular expression ?
0

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...

4 Comments

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.
It is possible to use it in that way: str_replace(array('tmp1', 'tmp2', ... 'tmp_n'), array($var1, $var2, ... $var_n), $content);
Note, preg functions eats huge count resources, where it possible we aren't need use them.
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; }

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.