私は PHP を学んでいて、単純な検索と置換スクリプトを作成しましたが、文字列の 0 の位置の後に来る文字に対してのみ機能するようです。
ここにスクリプトがあります:
<?php
$offset= 0;
if( isset ($_POST['text']) && isset($_POST['search']) && isset($_POST['replace']) ){
$text = $_POST['text'];
$search = $_POST['search'];
$replace = $_POST['replace'];
$searchlength = strlen($search);
if (!empty($text) && !empty($search) && !empty($replace)){
while ($strpos = strpos($text, $search, $offset)) {
echo$strpos;
$text = substr_replace($text, $replace, $strpos, $searchlength);
$offset = $strpos+$searchlength;
}
echo$text;
}else{
echo'Please fill in something';
}
}
?>
<hr>
<form action= "index.php" method = "POST">
<textarea name= 'text' rows= '20' cols = '20'></textarea><br>
Search for:<br>
<input type='text' name ='search'><br>
Replace with:<br>
<input type='text' name ='replace'><br>
<input type='submit' value='find and replace'>
</form>
何をしても、文字列の最初の文字または最初の単語を置き換えることはできません。