Q: using str_replace to replace braket "{"?

Q: using str_replace to replace braket "{"?

am 10.01.2008 07:33:50 von Sean Kim

From example in the book 'Programming PHP' (O'Reilly) (p.305)

$theKey = 'DESTINATION';
$target = '{' . $theKey . '}';
$inValues[$theKey] = 'some.php';

//$theTemplate = str_replace("\{$theKey}", $inValues[$theKey],
theTemplate);
//$theTemplate = str_replace("{$theKey}", $inValues[$theKey],
theTemplate);
$theTemplate = str_replace($target, $inValues[$theKey], $theTemplate);

--------------------------

First commented line is from book example but not working.
Second one is also not working too. It replaces string and left '{}'. So
result is '{some.php}'
Third one is my solution and it is working. It gives 'some.php'

But I think there is some way to just use str_replace alone.


Thank you

Sean Kim

Re: Q: using str_replace to replace braket "{"?

am 10.01.2008 12:46:33 von Michael Fesser

..oO(Sean Kim)

> From example in the book 'Programming PHP' (O'Reilly) (p.305)
>
>$theKey = 'DESTINATION';
>$target = '{' . $theKey . '}';
>$inValues[$theKey] = 'some.php';
>
>//$theTemplate = str_replace("\{$theKey}", $inValues[$theKey],
> theTemplate);
>//$theTemplate = str_replace("{$theKey}", $inValues[$theKey],
> theTemplate);
>$theTemplate = str_replace($target, $inValues[$theKey], $theTemplate);
>
>--------------------------
>
>First commented line is from book example but not working.

In a double-quoted string the { cannot be escaped. Try this:

$theTemplate = str_replace('{'.$theKey.'}', $inValues[$theKey],
$theTemplate);

Micha