0

「*」文字をほとんど含まない長い文字列があります。これらすべての文字を見つけて、それらとその後の 8 文字を置き換え、それらを別の文字列に置き換えたいと考えています。これを行う最善の方法は何ですか?これは私がやろうとした方法です:

 $over_string=strlen($story)-1;
 for ($i=0; $i<$over_string; $i++){
if($story[$i]=='*'){
     $id_substr=substr($story, $i+1,8);
     $name_player_change=some_function($id_substr);
     $id_to_replace='*'.$id_substr;
     $name_to_place='<a href="#">'.$name_player_change.'</a>';
     $story=str_replace($id_to_replace,$name_to_place,$story);
}//if
  }//for
4

1 に答える 1

1
$story = preg_replace('/\*.{8}/', $name_to_place, $story);

行きたい場所に連れて行ってくれるはずです。

または、リンクの内臓を * の後の 8 つの一致する文字に置き換えたい場合は、使用できます。

$story = preg_replace('/\*(.{8})/', '<a href="#">$1</a>', $story);
于 2013-07-10T15:11:22.120 に答える