preg_replace question
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
{stack开发者_运维知识库overflow 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
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) ) . '}';
精彩评论