php 在字符串之间替换,例如 „ 或 „或 „ 或 „

php replace between strings like „ or „ or „ or „

我想 php 替换这个字符串:

This String is called „Foo“ or „Bar“ but could be formatted as „Foo“ or even „Bar“ and many more combinations.

使用某种 BB 代码

This String is called [quote=Foo] or [quote=Bar] but could be formatted as [quote=Foo] or even [quote=Bar] and many more combinations.

我尝试用 php 中的 „“ 替换 ,但这根本不起作用。

我什至不知道如何开始:/

如有任何帮助,我们将不胜感激。

获取您的输入字符串,通过 un-encoding 规范化转义序列,然后一个简单的 RegEx 应该可以工作。


// Original string
$s1 = <<<EOD
This String is called „Foo“ or „Bar“ but could be formatted as &ldquor;Foo&#8220; or even &#8222;Bar&#x0201C; and many more combinations.
EOD;

// Normalize entities
$s2 = html_entity_decode($s1, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5);

// Convert to bbCode-like style
$s3 = preg_replace(
    '/„(.*?)“/',
    '[quote=]',
    $s2
    );

print_r($s3);

此处演示:https://3v4l.org/Dfig0