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:(

<?php
$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!

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);
Member Avatar for diafol
$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.

COnfused.

@ardav
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.

Member Avatar for diafol

OK, solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.