PHP 替换字符串,如 " 或 „ 或 " 或 "



我喜欢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中用„“替换,但根本不起作用。

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

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

获取输入字符串,通过取消编码来规范转义序列,然后一个简单的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=$1]',
$s2
);
print_r($s3);

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

最新更新