1

I am using PHP 7.2.4, I want to make a template engine project, I try to use preg_replace to change the variable in the string, the code is here:

<?php
$lang = array(
    'hello' => 'Hello {$username}',
    'error_info' => 'Error Information : {$message}',
    'admin_denied' => '{$current_user} are not Administrator',
);

$username = 'Guest';
$current_user = 'Empty';
$message = 'You are not member !';

$new_string = preg_replace_callback('/\{(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/', 'test', $string);

function test($matches)
{
    return '<?php echo '.$matches[1].'; ?>';
}

echo $new_string;

But it just show me

Hello , how are you?

It automatically remove the variable...

Update: here is var_dump:

D:\Wamp\www\t.php:5:string 'Hello <?php echo $username; ?>, how are you?' (length=44)
3
  • preg_replace_callback Commented Jun 18, 2018 at 11:16
  • This is an example how you can do it. Define an associative array rather than separate variables, and use it to replace matches. Commented Jun 18, 2018 at 12:14
  • 1
    @WiktorStribiżew Yes ! thanks you very much~ Commented Jun 19, 2018 at 1:32

3 Answers 3

1

You may use create an associative array with keys (your variables) and values (their values), and then capture the variable part after $ and use it to check in the preg_replace_callback callback function if there is a key named as the found capture. If yes, replace with the corresponding value, else, replace with the match to put it back where it was found.

Here is an example code in PHP:

$values = array('username'=>'AAAAAA', 'lastname'=>'Smith');
$string = 'Hello {$username}, how are you?';
$new_string = preg_replace_callback('/\{\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)}/', function($m) use ($values) {
        return 'Hello <?php echo ' . (!empty($values[$m[1]]) ? $values[$m[1]] : $m[0]) . '; ?>';
    }, $string);

var_dump($new_string);

Output:

string(47) "Hello Hello <?php echo AAAAAA; ?>, how are you?"

Note the pattern charnge, I moved the parentheses after $:

\{\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)}
    ^                                        ^

Actually, you may even shorten it to

\{\$([a-zA-Z_\x7f-\xff][\w\x7f-\xff]*)}
                        ^^
Sign up to request clarification or add additional context in comments.

Comments

0

Do you want something like this?

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

The result is:

Hello AAAAAA, how are you?

The more simple way would be to just write

<?php
$username = 'AAAAAA';
$string = 'Hello '.$username.', how are you?';

1 Comment

No, I want to make an template engine class, the variable's name should change sometimes, so I need to let it can be Hello <?php echo $username; ?>, how are you?
0

I'm a fan of keeping it simple so I would use str_replace since it will also change all instances which may come in handy as you go forward.

$string = 'Hello {$username}, how are you?';
$username = 'AAAAAA';
echo str_replace('{$username}',$username,$string);

1 Comment

I am using it in my template engine project, the variables will be too much to write all variables with str_replace function

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.