Here we have the code, let's say (the text in $ var may change and other tags may appear there, not just the 'a', but maybe they will not be there at all):
$var = "slovo <a href='hashcode.ru'>slovo</a> slovo"; echo substr_replace($var, 'bob', 8, 0) . "<br />\n";
As a result, substr_replace will break the html tag "a" and fit into it. Such porridge will turn out:
slovo <abobashcode.ru'>slovo</a> slovo<br />
How to do to temporarily remove the formatting from the text, and then, when substr_replace finishes, put back all the formatting tags. Simply put, how to get this result:
slovo <a href='hashcode.ru'>slovo</a>bob slovo
Note: you need to insert text after the closing html tag, whatever it is.