If my string is:
Once upon a midnight dreary,
while I pondered, weak and weary,
Over many a quaint and curious volume of forgotten lore,
And I wanted to highlight the following strings (sorted by string length to prefer longer string matches) :
ponder,weary,weak,ary,red,we
I want to end up with:
Once upon a midnight dre<span>ary</span>,
while I <span>ponder</span>ed, <span>weak</span> and <span>weary</span>,
Over many a quaint and curious volume of forgotten lore,
How can I avoid (as a simple str_replace()
might lead me)?
Once upon a midnight dre<span>ary</span>,
while I <span>ponde<span>r</span>ed</span>, <span><span>we</span>ak</span> and <span><span>we</span><span>ary</span></span>,
Over many a quaint and curious volume of forgotten lore,