>ok.所以我有一个数组,其中包含我需要在字符串中找到并替换
的单词使用他们的键(数组中该单词的键)。
在英语中,这是有效的。
function replace_twophrase_words($string) {
$string = strtolower($string);
$replacements = array (
'movers' => 'Moving Services',
'home-moving' => 'Home Moving',
'commercial-moving' => 'office-moving',
);
$string = str_replace($replacements, array_keys($replacements), $string);
}
希伯来数组(在评论中询问):
$replacements = array (
'עיצוב-פנים' => 'עיצוב פנים',
'עיצוב-פנים' => 'מעצבת פנים',
'עיצוב-פנים' => 'עיצוב משרדים',
);
但。。。这在希伯来语中似乎根本不起作用...
任何人都可以抛出指针/一些示例代码吗?
preg_replace可能是一个很好的替代品,但我不知道如何为这种情况编写模式(所以它也是可选的)
感谢您的帮助。
您需要使用 mb_ereg_replace:
setlocale( LC_CTYPE, 'en_US.UTF-8' );
header( 'Content-Type: text/plain; charset=utf-8' );
echo mb_ereg_replace( 'עיצוב-פנים', 'עיצוב פנים', 'Hebrew string is here: עיצוב-פנים ; and back to Latin.' );
这是因为希伯来字母由多个字节组成,str_reaplce和preg_replace(默认情况下)不理解这些。