2013-07-10 92 views
0

我有一個很長的字符串,包含幾個「*」字符,我想找到所有這些字符,並替換他們+後8個字符,並用花葯字符串替換它們。做這個的最好方式是什麼?這就是我試圖做:你想要去PHP找到並替換字符串後特定字符

$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 
+0

你能發表一個原始字符串的例子嗎? –

回答

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

應該得到你。

或者如果你想用*後的8個匹配字符替換鏈接的膽量,那麼你可以使用。

$story = preg_replace('/\*(.{8})/', '<a href="#">$1</a>', $story);