php 中的简单字符串突变
simple string mutation in php
对于我的代码,如果以下单词是 "red",我只想进行字符串突变。不,它背后没有任何逻辑,但它应该是一个困难案例的简单案例。
所以因此我使用了 next()
但如果最后一个词是 "red" 那么它不起作用。
我的代码:
$input = ['man', 'red', 'apple', 'ham', 'red'];
$endings = ['m', 'n'];
$shouldRemove = false;
foreach ($input as $key => $word) {
// if this variable is true, it will remove the first character of the current word.
if ($shouldRemove === true) {
$input[$key] = substr($word, 1);
}
// we reset the flag
$shouldRemove = false;
// getting the last character from current word
$lastCharacterForCurrentWord = $word[strlen($word) - 1];
if (in_array($lastCharacterForCurrentWord, $endings) && next($input) == "red") {
// if the last character of the word is one of the flagged characters,
// we set the flag to true, so that in the next word, we will remove
// the first character.
$shouldRemove = true;
}
}
var_dump($input);
正如最后提到的 "red" 而不是 "ed" 我得到 "red"。我应该怎么做才能获得所需的输出?
你可以select下一个键"manually":
$input = ['man', 'red', 'apple', 'ham', 'red'];
$endings = ['m', 'n'];
$shouldRemove = false;
foreach ($input as $key => $word) {
// if this variable is true, it will remove the first character of the current word.
if ($shouldRemove === true) {
$input[$key] = substr($word, 1);
}
// we reset the flag
$shouldRemove = false;
// getting the last character from current word
$lastCharacterForCurrentWord = $word[strlen($word) - 1];
if (in_array($lastCharacterForCurrentWord, $endings) && $input[$key+1] == "red") {
// if the last character of the word is one of the flagged characters,
// we set the flag to true, so that in the next word, we will remove
// the first character.
$shouldRemove = true;
}
}
var_dump($input);
array(5) { [0]=> string(3) "man" [1]=> string(2) "ed" [2]=> string(5) "apple" [3]=> string(3) "ham" [4]=> string(2) "ed" }
它不起作用的原因是它依赖于循环的下一次迭代来根据您在当前迭代中的评估来执行您需要的操作。如果您要更改的项目是数组中的最后一项,则不会有下一次迭代来更改它。
您可以跟踪前面的单词并使用它,而不是检查后面的单词。
$previous = '';
foreach ($input as $key => $word) {
if ($word == 'red' && in_array(substr($previous, -1), $endings)) {
$input[$key] = substr($word, 1);
}
$previous = $word;
}
对于我的代码,如果以下单词是 "red",我只想进行字符串突变。不,它背后没有任何逻辑,但它应该是一个困难案例的简单案例。
所以因此我使用了 next()
但如果最后一个词是 "red" 那么它不起作用。
我的代码:
$input = ['man', 'red', 'apple', 'ham', 'red'];
$endings = ['m', 'n'];
$shouldRemove = false;
foreach ($input as $key => $word) {
// if this variable is true, it will remove the first character of the current word.
if ($shouldRemove === true) {
$input[$key] = substr($word, 1);
}
// we reset the flag
$shouldRemove = false;
// getting the last character from current word
$lastCharacterForCurrentWord = $word[strlen($word) - 1];
if (in_array($lastCharacterForCurrentWord, $endings) && next($input) == "red") {
// if the last character of the word is one of the flagged characters,
// we set the flag to true, so that in the next word, we will remove
// the first character.
$shouldRemove = true;
}
}
var_dump($input);
正如最后提到的 "red" 而不是 "ed" 我得到 "red"。我应该怎么做才能获得所需的输出?
你可以select下一个键"manually":
$input = ['man', 'red', 'apple', 'ham', 'red'];
$endings = ['m', 'n'];
$shouldRemove = false;
foreach ($input as $key => $word) {
// if this variable is true, it will remove the first character of the current word.
if ($shouldRemove === true) {
$input[$key] = substr($word, 1);
}
// we reset the flag
$shouldRemove = false;
// getting the last character from current word
$lastCharacterForCurrentWord = $word[strlen($word) - 1];
if (in_array($lastCharacterForCurrentWord, $endings) && $input[$key+1] == "red") {
// if the last character of the word is one of the flagged characters,
// we set the flag to true, so that in the next word, we will remove
// the first character.
$shouldRemove = true;
}
}
var_dump($input);
array(5) { [0]=> string(3) "man" [1]=> string(2) "ed" [2]=> string(5) "apple" [3]=> string(3) "ham" [4]=> string(2) "ed" }
它不起作用的原因是它依赖于循环的下一次迭代来根据您在当前迭代中的评估来执行您需要的操作。如果您要更改的项目是数组中的最后一项,则不会有下一次迭代来更改它。
您可以跟踪前面的单词并使用它,而不是检查后面的单词。
$previous = '';
foreach ($input as $key => $word) {
if ($word == 'red' && in_array(substr($previous, -1), $endings)) {
$input[$key] = substr($word, 1);
}
$previous = $word;
}