0

I have a long string that contain few "*" chars, I want to find all those chars and replace them+the 8 chars after them, And replace them with anther string. What is the best way to do this? this is how i tried to do:

 $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
1
  • Can you post an example of what the original string looks like? Commented Jul 10, 2013 at 15:08

1 Answer 1

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

should get you where you want to go.

or if you want to replace the guts of the link with the 8 matching characters after the * then you can use.

$story = preg_replace('/\*(.{8})/', '<a href="#">$1</a>', $story);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.