如何使用下面的代码翻译句子
输入 Hello World
输出 世界你好
使用此代码
while ($line = trim(fgets(STDIN))) {
//$line contains the line of the input
echo $line . "\n";
}
答案 0 :(得分:3)
explode()
后跟array_reverse()
,然后是implode()
$string = 'hello world i like php';
$string = implode(' ',array_reverse(explode(' ',$string)));
echo $string; // php like i world hello
答案 1 :(得分:0)
方法1:
<?php
$str = "Hello World";
$i = 0;
while( $d = $str[$i] )
{
if( $d == " "){
$out = " ".$temp.$out;
$temp = "";
}else{
$temp.=$d;
}
$i++;
}
echo $temp.$out;
?>
方法2:
$s = "Hello World";
// break the string up into words
$words = explode(' ',$s);
// reverse the array of words
$words = array_reverse($words);
// rebuild the string
$s = join(' ',$words);
print $s;
方法3:
$reversed_s = join(' ',array_reverse(explode(' ',$s)));