Replace function based on the length
sub replace { ($text, $to_replace, $replace_with) = @_; substr ($text, index($text, $to_replace), length($to_replace), $replace_with); return $text; } print replace("Here is the text.", "text", "word");