views:

59

answers:

1

Hello I'm trying to do the following:

Say I have an input string like this

{stackoverflow is a {cool|great} website|stackoverflow is the {best|greatest}}. {stackoverflow is for {cool|great} coders|stackoverflow is the {best|greatest} site for coders}

How can I convert it to the following format by using PHP

{stackoverflow is a [cool|great] website~stackoverflow is the [best|greatest]}. {stackoverflow is for [cool|great] coders~stackoverflow is the [best|greatest] site for coders}

I'm playing with preg_replace but I can't find a working solution.

To sum things up: The nested { should be turn into [.

And the | on the first level should turn into ~.

Any ideas?

Regards

A: 

no need for preg_replace ;)

$value = '{stackoverflow is a {cool|great} website|stackoverflow is the {best|greatest}}';
$map = array( '{' => '[', '}' => ']', '~' => '|' );
$value = '{' . str_replace( array_keys($map), array_values($map), substr($value, 1, -1) ) . '}';
nathan
The `trim` removes too many `}` at the end of the string.
salathe
That doesn't seem to work...Did an echo on $value, and it spit out this:{stackoverflow is a [cool|great] website|stackoverflow is the [best|greatest}
xil3
that's not realy working ;(.
brechtvhb
whoops sorry about that, should of tested - sorted now :)
nathan