开发者

preg_replace question

开发者 https://www.devze.com 2023-01-06 09:53 出处:网络
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|

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) ) . '}';
0

精彩评论

暂无评论...
验证码 换一张
取 消