文の最初の単語が「The」の場合、最後までカンマを前に置きたいので、たとえば、映画のタイトルが「The Hunger Games」の場合、「Hunger Games, The」のようにします。
これは可能ですか?
文の最初の単語が「The」の場合、最後までカンマを前に置きたいので、たとえば、映画のタイトルが「The Hunger Games」の場合、「Hunger Games, The」のようにします。
これは可能ですか?
これを試してください:
<?
$str = 'The Hunger Games';
if (strtolower(substr($str,0,3)) == "the"){
$str = trim(substr($str,3)).', The';
}
echo $str;
?>
使用する:
echo custom_func('The Hunger Games');
関数:
function custom_func($s) {
$s = trim(preg_replace('~\s+~', ' ', $s));
$a = explode(' ', $s);
if (strtolower($a[0]) != 'the' || count($a) < 2) return $s;
$the = array_shift($a);
return implode(' ', $a) . ', ' . $the;
}
デモ。