-1

My Example:

$name = "Simon";
$string = "My name is [name].";
echo preg_replace("/\[(.*)]/", ${"$1"}, $string);
// Expected: My name is Simon.
// I get: My name is .
// ${"$1"} should be $name?
exit();

When I do only:

echo preg_replace("/\[(.*)]/", "$1", $string);
// I get: My name is name.
// $1 = name

What am i doing wrong? Why is PHP not using the generated $name var? This is only a example. I would like to work this with any replace:

[foo] --> $foo
[bar] --> $bar
...
user2987790
  • 135
  • 8
  • echo str_replace("[name]",$name,$string); – Asik Nov 20 '14 at 17:50
  • Hey Asik, i don't want to use str_replace because i want to do it with much more vars. I want to work this with any replace. `[foo] > $foo`, `[bar] > $bar`, etc.. I don't want to use arrays. – user2987790 Nov 20 '14 at 17:55
  • 1
    Is there a particular reason why you're wanting to use `preg_replace()` etc. and not just `$string = "My name is " .$name;`? This sounds like overkill if you just want to echo out/populate a username. – Funk Forty Niner Nov 20 '14 at 17:57
  • Explanation is not clear. And lot of edits...? – Indra Kumar S Nov 20 '14 at 18:03
  • I have much more Placeholders, not only [name]. I could put them all in arrays and do str_replace(). It seems for me that the automatic preg_replace() with `$1` would be the more elegant solution. – user2987790 Nov 20 '14 at 18:06
  • 1
    In order for this to work, you will need to change `[name]` to `[$name]` and `${"$1"}` to `"$1"`. Least, from what I could gather. Otherwise, you will need to modify something. If you can live with that, great. – Funk Forty Niner Nov 20 '14 at 18:15
  • Fred -ii- this is great man, you can post this as an answer. But why is this `${"$1"}` not working? Do you have any clue where's the problem for PHP? – user2987790 Nov 20 '14 at 18:20
  • @user2987790 Because, you already have one `$`. – Funk Forty Niner Nov 20 '14 at 18:22
  • @user2987790 It has been done. – Funk Forty Niner Nov 20 '14 at 18:27

3 Answers3

4

As per OP's wish to post my comment as an answer:

In order for this to work, you will need to change [name] to [$name] and ${"$1"} to "$1"

$name = "Simon";
$string = "My name is [$name].";

echo preg_replace("/\[(.*?)]/", "$1", $string);

PHP needs a variable to go on, so using [name] isn't being populated.


As per another and earlier comment I made, alternatively you could very well do:

$name = "Simon";
$string = "My name is " .$name;

echo $string;

If you have a framework with existing brackets, then that is something you haven't told us, only that you said in comments:
" have much more Placeholders, not only [name]", whether it's part of something bigger, then stick to the accepted method.


As per in comments, you could alternatively use "/\[([^\]]+)\]/" instead of "/\[(.*)]/"
Or "/[(.*?)]/" :)

in regards to using for example: "[$foo]bar[$foo]"

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

I suggest you to use something like this:

class Tpl {
  private $tpl;
  public function __construct($tpl) {
     $this->tpl = $tpl;
  }
  public function render($data) {
    $result = $this->tpl;
    foreach ($data as $key => $value) {
      $result = str_replace("%%$key%%", str_replace('%%', '', $value), $result);
    }
    return $result;
  }
}

$greet = new Tpl('Hello, %%name%%');
echo $greet->render(array('name' => 'World'));

But you always must care, that you correctly escape your placeholder formatting. I just remove it :)

vp_arth
  • 14,461
  • 4
  • 37
  • 66
  • Finally i formatted the vars in mysql with placeholders {{FOO}} and used [strtr()](http://stackoverflow.com/questions/10106052/replace-multiple-placeholders-with-php). Thank You All for Your Help! So there is no possibility to store "real" php Vars mixed with Text in mysql? I understand it from the point of security issues but there should be and option to to this. – user2987790 Nov 20 '14 at 20:21
0

No need in preg_replace at all.
You use variable_names anyway - so php already has this placeholders - "{$var}":

$name = "Simon";
$string = "My name is {$name}.";
vp_arth
  • 14,461
  • 4
  • 37
  • 66
  • That's what [**I wrote**](http://stackoverflow.com/questions/27046192/replace-with-same-named-php-variable#comment42610960_27046192) but the OP seems to want to use that for a particular reason. Why, I have no idea. – Funk Forty Niner Nov 20 '14 at 18:32
  • in a mysql field i store text mixed with PHP Vars: `bla $foo bla $bar`. If i store this mysql result and echo it i get the same: `bla $foo bla $bar`. if i store text with placeholders `[foo], [bar]` and change them later to $PHPVars the result should be `bla FOOVAR-CONTENT bla BARVAR-CONTENT` (Before i echo the mysql the vars are already set in the script) – user2987790 Nov 20 '14 at 18:48
  • 1
    So the OP did have a "special" reason ;) – Funk Forty Niner Nov 20 '14 at 18:52
  • So whats should i do instead? I want to get the already set values of the vars into my mysql-result ? – user2987790 Nov 20 '14 at 18:52
  • No reason... no any profit... no difference from `a {$v} b {$v}` to `a [v] b [v]` if you need `$v` variable to make it works... – vp_arth Nov 20 '14 at 18:52
  • Why your database knows about PHP anything? It's about data, not about implementation. – vp_arth Nov 20 '14 at 18:55
  • PHP gets a mysql-Result like `$mailbody = "Hello $name";`. `$name` is defined before mysql-request. if i echo `$mailbody` my result is still `"Hello $name"` instead of `"Hello MAILBODYCONTENT"`. When i do it the way with placeholders it works.. – user2987790 Nov 20 '14 at 19:01