$content = “[:ja]りんご[:en]apple”
というフォーマットの文字列に対して、$localeで指定した言語に対応した文字列を返す。
public static function getLocaleContent($content, $locale){
if (empty($content)) {
return "";
}
if (empty($locale)) {
return $content;
}
$langs = array(
'ja',
'en'
);
preg_match('/(\[:(.+)\](.*))(\[:(.+)\](.*))/', $content, $matches);
$len = count($matches);
$len_divided_3 = ($len-1)/3 ;
if(empty($matches) | $len_divided_3 < count($langs) ){
return $content;
}
for($i=0; $i<$len_divided_3; $i++){
$cotents[ $langs[$i] ] = $matches[3*($i+1)];
}
$locale_content = null;
if(array_key_exists($locale, $cotents)){
$locale_content = $cotents[$locale];
}
return $locale_content;
}
$len_divided_3 を使う意味は、preg_matchで対応する文字列を$matchesniに抽出したときに,
$matches[1] = [:ja]りんご $matches[2] = ja $matches[3] = りんご $matches[4] = [:en]apple $matches[5] = en $matches[6] = apple
左から順番に検索していくので上のような順番になって、ほしいのは3の倍数の要素だから。


