确定。所以我有一个数组,其中包含我需要在字符串中找到的单词并替换为 用他们的键(数组中的那个词)。
在英语中,这有效。
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可能是一个很好的替代品,但我不知道如何为这种情况编写模式(所以它也是可选的)
感谢您的帮助。
答案 0 :(得分:0)
您需要使用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(默认情况下)不理解这些。