2013-10-15 71 views

回答

0

请试试这个:

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

WORKING CODE

+0

如果用户输入“THE END”,该怎么办?他会得到输出“结束,”...... –

+0

@Glavić只是用''替换'',''。 substr($ str,0,3)' –

+1

@WimMostmans:我知道如何制作工作代码,但答案应该对未来的新人有效。 –

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

Demo

+1

用于do_magic函数名称。 -1表示do_magic函数名称。 –

+0

好吧,你已经删除了+1部分,但-1仍然在这里。尝试一个有意义的函数名称:) –

+0

@STTLCU:我认为这取决于用户重命名函数名称,答案只是为了演示。我可以很容易地编写这个例子,而不使用函数... –

相关问题