-1

Since ereg replace is depreciated, i would like to know how to use preg instead. Here is my code and i need to replace { } tags.

$template = ereg_replace('{USERNAME}', $info['username'], $template);
    $template = ereg_replace('{EMAIL}', $info['email'], $template);
    $template = ereg_replace('{KEY}', $info['key'], $template);
    $template = ereg_replace('{SITEPATH}','http://somelinkhere.com', $template);

It won't work if i just switch it to preg replace.

Francis Laclé
  • 384
  • 2
  • 6
  • 22
user2802518
  • 17
  • 1
  • 2

2 Answers2

2

Use str_replace(), why not?

Like this, wow:

<?php
$template = str_replace('{USERNAME}', $info['username'], $template);
$template = str_replace('{EMAIL}', $info['email'], $template);
$template = str_replace('{KEY}', $info['key'], $template);
$template = str_replace('{SITEPATH}','http://somelinkhere.com', $template);
?>

Works like a charm.

Paul Denisevich
  • 2,329
  • 14
  • 19
0

I don't know how ereg_replace work but preg_replace works regular expressions.

If you want to replace "{" and "}"

The correct way will be:

$template = preg_replace("/({|})/", "", $template);
// if $template == "asd{asdasd}asda{ds}{{"
// the output will be "asdasdasdasdads"

Now, if you would like to replace "{" and "}" only where is something specific inside them you should execute:

$user = "whatever";
$template = preg_replace("/{($user)}/", "$0", $template);
// if $template == "asd{whatever}asda{ds}"
// the output will be "asdwhateverasda{ds}"

If you want to replace "{" and "}" with something that could be any string with only letters from "a" to "Z"

You should use:

$template = preg_replace("/{([a-Z]*)}/", "$0", $template);
// if $template == "asd{whatever}asda{ds}{}{{{}"
// the output will be "asdwhateverasdads{}{{{}"