【问题标题】:Check if piece of string exists inside array检查数组中是否存在一段字符串
【发布时间】:2012-05-09 06:46:57
【问题描述】:

我正在使用这个功能来翻译东西:

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'
}

我希望脚本能够翻译给定字符串中数组键中的部分。这可能吗?

【问题讨论】:

  • 那么mostseen12345 会变成mavistos12345 吗?

标签: php arrays string translate


【解决方案1】:

您可以在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);

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 2020-07-06
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多