-5

文の最初の単語が「The」の場合、最後までカンマを前に置きたいので、たとえば、映画のタイトルが「The Hunger Games」の場合、「Hunger Games, The」のようにします。

これは可能ですか?

4

2 に答える 2

0

これを試してください:

<?
$str = 'The Hunger Games';
if (strtolower(substr($str,0,3)) == "the"){
    $str = trim(substr($str,3)).', The';
}
echo $str;
?>

作業コード

于 2013-10-15T11:41:16.727 に答える
0

使用する:

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

デモ

于 2013-10-15T11:40:14.283 に答える