[edit] already too slow... 😃 [/edit]
I think, after calc the point, you can search for any opened tag in before string. If there isn't well, else you must look for any ">" in the next string. Something like this:
$pos_into_insert=my_calc_function($mystring); // do your calc to find %
$bef_string=substr($mystring,0,$pos_into_insert);
$aft_string=substr($mystring,$pos_into_insert,strlen($mystring));
$pos_start = strrpos($bef_string, "<");
if ($pos_start !== false) { // may be we are in tag
$pos_end = strrpos($bef_string, ">");
if($pos_start>$pos_end){
// we are inside a TAG
$pos_into_insert = strpos($aft_string, ">")+1;
} else {
// is ok
}
}
$new_mystring=substr($mystring,0,$pos_into_insert).
"<img src='blabla.gif'>".
substr($mystring,$pos_into_insert,strlen($mystring));
This is may be a starting point. Must check possibily error, like if the tag is not closed...
I don't know if all this can do easily with reg.exp. or other function. This is just any possible solution.
see you