文字列内の部分文字列のすべてのインスタンスを検索する方法はありますか?これは私がこれまでに持っているものですが、「ed」サブストリングを検索すると、最初のサブストリングが見つかると検索を停止するようです。したがって、エンディングを変更しようとしても、「ヘッジ」という単語は影響を受けません。
<?php
$wordList2 = array('kissed','hoped','learned','wanted','sounded', 'hedged');
for ($i = 0; $i<sizeof($wordList2);$i++){
$wrongAnswers2[$wordList2[$i]] = array();
}
for ($i = 0; $i < sizeof($wordList2); $i++){
if(strpos($wordList2[$i], 'ed')===(strlen($wordList2[$i])-2)) {
$pos = strpos($wordList2[$i], 'ed');
if(
substr($wordList2[$i], -3, 1) ==='p' ||
substr($wordList2[$i], -3, 1) ==='f' ||
substr($wordList2[$i], -3, 1) ==='s' ||
substr($wordList2[$i], -3, 1) ==='k' ||
substr($wordList2[$i], -3, 1) ==='h'
){
$replacement = substr_replace($wordList2[$i], 't', $pos,2);
array_push($wrongAnswers2[$wordList2[$i]],$replacement);
} else if
(
substr($wordList2[$i], -3, 1) ==='b' ||
substr($wordList2[$i], -3, 1) ==='v' ||
substr($wordList2[$i], -3, 1) ==='z' ||
substr($wordList2[$i], -3, 1) ==='g' ||
substr($wordList2[$i], -3, 1) ==='n'
){
$replacement = substr_replace($wordList2[$i], 'd', $pos,2);
array_push($wrongAnswers2[$wordList2[$i]],$replacement);
} else if
(
substr($wordList2[$i], -3, 1) ==='d' ||
substr($wordList2[$i], -3, 1) ==='t'
){
$replacement = substr_replace($wordList2[$i], 'id', $pos);
array_push($wrongAnswers2[$wordList2[$i]],$replacement);
}
}
}
?>
これが私が得た出力です。基本的に、プログラムに「ヘッジ」の終わりを変更させる方法が必要です。ありがとう。
Array
(
[kissed] => Array
(
[0] => kisst
)
[hoped] => Array
(
[0] => hopt
)
[learned] => Array
(
[0] => learnd
)
[wanted] => Array
(
[0] => wantid
)
[sounded] => Array
(
[0] => soundid
)
[hedged] => Array
(
)
)。