简体   繁体   English

如何在PHP中将前两个单词移到后面的两个单词?

[英]How to Move the first two words to the two words thereafter in PHP?

I know this is kinda weird, but I need this to complete my task . 我知道这有点奇怪, 但我需要这个来完成我的任务

I want to move the first two words to the two words thereafter, example is in my (error) code : 我想将前两个单词移到后面的两个单词中,例如在我的(错误)代码中:

<?
$sentence = "zero one two three four five six seven eight";
$sentence2 = explode (" ",$sentence);
$total = count($sentence2);
for ($i = 4; $i < $total; ++$i) {
$result = $sentence2[2]." ".$sentence2[3]." ".$sentence2[0]." ".$sentence2[1]."  ".$sentence2[$i];
}
echo "Original sentence : ".$sentence;
echo "<br>Result : ".$result;
?>

but the result from that code is not what i want, the result is 但是该代码的结果不是我想要的结果

two three zero one eight

i want the result : 我想要的结果:

two three zero one four five six seven eight

can you help me make a better code? 你能帮我做一个更好的代码吗?

Each time the code inside your loop runs, the $result variable receives a new value. 每次循环中的代码运行时, $result变量都会收到一个新值。

You should only append words at the end of the sequence to it. 您只应在序列末尾添加单词。

So, replace you for loop by this: 所以,用这个替换你for循环:

$result = $sentence2[2]." ".$sentence2[3]." ".$sentence2[0]." ".$sentence2[1];
for ($i = 4; $i < $total; ++$i) {
    $result .= "  ".$sentence2[$i];
}

You can also use array_splice for this case 在这种情况下,您也可以使用array_splice

$sentence = "zero one two three four five six seven eight";
$words    = explode(" ",$sentence,3);
$base     = explode(" ",$words[2]);
array_splice($base,2,0,array($words[0],$words[1]));
echo implode(" ",$base);

or one line solution,:-) 或一线解决方案,:-)

echo preg_replace('#^(\w+\s+)(\w+\s+)(\w+\s+)(\w+\s+)#','$3$4$1$2',$sentence);

The problem is that you overwrite the result all the time. 问题是您始终覆盖结果。 So when it steps through your for-loop the first time the string will be 因此,当第一次执行字符串时,它会逐步执行for循环

two three zero one five

The second time it will be 它将是第二次

two three zero one six

etc. 等等

But you will only see it ending by eight because you output the string only at the end. 但是你只会看到它以8结尾,因为你只在最后输出字符串。 You should store your new string in a variable and append your next number to that. 您应该将新字符串存储在变量中,并将下一个数字附加到该变量中。 It should read something like; 它应该读起来像;

<?
$sentence = "zero one two three four five six seven eight";
$sentence2 = explode (" ",$sentence);
$total = count($sentence2);
$result = $sentence2[2]." ".$sentence2[3]." ".$sentence2[0]." ".$sentence2[1]."  ";
for ($i = 4; $i < $total; ++$i) {
$result = $result."  ".$sentence2[$i];
}
echo "Original sentence : ".$sentence;
echo "<br>Result : ".$result;
?>

This does the trick nicely. 这很好地解决了这个问题。

$sentence = "zero one two three four five six seven eight";
$sentenceParts = explode (" ",$sentence);

$itemCount = count($sentenceParts);
$result = $sentenceParts[2]." ".$sentenceParts[3]." ";
for($i = 0; $i < $itemCount; $i++) {
    if($i != 2 && $i !=3) {
        $result .= $sentenceParts[$i]." ";
    }
}

echo $result;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM