Hi there, DaniWeb.

I have been struggling with this one for a while.

How do i write a regexp that matches every {$*} wherein * is wildcard.

I have written one: /\{\$(.*)\}/ and it works fine at regexpal.com, but not with my preg_replace:(

$string = "Hello {$name}! How are you?";

$pattern = "/\{\$(.*)\}/";

$replace = "${1}";

$newstring = preg_replace($pattern,$replace,$string);

echo $newstring;

However, the output should then be "Hello name! How are you?"

but instead, the output isj ust "Hello {$name}! How are you?"

No errors, just the text without anything replaced. Can anybody please help me?:)

Thank you alot!

5 Years
Discussion Span
Last Post by diafol

Have you tried this ?

$newstring = preg_replace('/\{\$(.*?)\}/', '\1', $string);

// These two both work on my local server too
$newstring = preg_replace('/\{\$(.*?)\}/', '$1', $string);
$newstring = preg_replace('/\{\$(.*?)\}/', '${1}', $string);

Edited by pritaeas: n/a

$string = "Hello {$name}! How are you?";

Won't $string take that as Hello !How are you? since $name probably doesn't exist. If you echo $string, that's what you'll get, along with an error message.

If you use single quotes:

$string = 'Hello $name! How are you?';

That'll be different.

However, I don't see how this works. You're passing, for instance:

$Peter or $Paul and want to end up with Peter or Paul.



It is just temporary, i have found a solution.

What i wanted to do was:

$string = 'Hi there {$name}!';

and then the script would replace that {$name} with $TPLCLASS->getVariable("name"); hence the replacement for $name should be name.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.