We don't offer a solution for this.
Basically you would want to isolate the search terms into an array.
Then write a function that probably takes the search term array and the starting text as an argument and returns the text with the added spans.
Then you would use a regular expression match while looping through the array to do the replacement. Since you only pass in the content, there are no html tags to ignore, but you could use a regular expression to remove tags first before doing replacements if necessary as well.