【发布时间】:2014-05-02 17:54:23
【问题描述】:
我只需要使用一组键及其翻译进行简单的字符串翻译(url)。
尝试这样:
function ruta_iso( $ruta ) {
$slugs = array(
'noticia' => array(
'es' => 'noticia',
'en' => 'post'
),
'pregunta' => array(
'es' => 'pregunta',
'en' => 'question'
),
'consejo' => array(
'es' => 'consejo',
'en' => 'tip'
),
'noticias' => array(
'es' => 'noticias',
'en' => 'news'
)
);
$idioma_defecto = 'es';
$idioma = 'en' ;
if ( ($idioma != $idioma_defecto) && ($ruta == '/') ) {
return "/$idioma";
} else if( $idioma != $idioma_defecto ){
foreach($slugs as $key => $slug){
if ( ( strpos("/$key/", $ruta ) === false ) ){
}else {
$ruta = str_replace( "/$key/", '/'.$slug[$idioma].'/' , $ruta );
}
}
$ruta = "/$idioma$ruta";
} else {
}
return $ruta;
}
echo '-------Ruta Iso '.ruta_iso('/noticias/'); /* Works! */
echo ' -------Ruta Iso '.ruta_iso('/noticias/noticia/'); /* Nope.. */
可以在这里测试:
它似乎只为一只蛞蝓完成这项工作,但如果不止一只,甚至:
echo ' -------Ruta Iso '.ruta_iso('/noticias/blabla/'); /* Nope.. */
所以我不确定如何尝试,我的意思是,我没有破坏 foreach,为什么不检查每个字符串?
有吗?
【问题讨论】:
标签: php arrays foreach str-replace