[英]Check if piece of string exists inside array
我正在使用此功能來翻譯內容:
function t($string)
{
global $_ACTIVE_LANGUAGE;
if(is_array($_ACTIVE_LANGUAGE) && array_key_exists($string,$_ACTIVE_LANGUAGE) )
{
return (!empty($_ACTIVE_LANGUAGE[$string])) ? $_ACTIVE_LANGUAGE[$string] : $string;
} else {
return $string;
}
}
它很好用,我放了t('hola'),如果英語文件中的數組為'hola'=>'hello',它會翻譯它。
但是,現在我希望能夠翻譯的字符串可能包含比字符串更多的文本,例如以下示例:
$string1 = 'download-file-justin-bieber-awesome-voice.html';
$string2 = 'view-file-rihanna-very-sexy.html';
$string3 = 'mostseen12345.html';
$string4 = 'incredible:stuff-and:real-things.html';
$array = array
(
'download-file' => 'descargar-archivo',
'view-file' => 'ver-archivo',
'mostseen' => 'masvistos',
'incredible:stuff' = 'cosas:increibles'
}
我希望腳本能夠翻譯給定字符串中數組鍵中的部分。 這有可能嗎?
您可以在php-manual上查看str_replace()
。
$from = array('download-file','view-file','mostseen','incredible:stuff');
$to = array('descargar-archivo','ver-archivo','masvistos','cosas:increibles');
$translated_string = str_replace($from,$to,$original_string);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.