I'm trying to wrap words and words sequence from a given list with preg_replace. It almost works, but there are some use cases it doesn't and I can't figure it how.
For instance I do this:
// sort by descending length
usort($this->_keywords, function($a,$b){return(strlen($a)<strlen($b));});
// wrapper is -%string%-
foreach ($this->_keywords as $keyword) {
$value = preg_replace('/((?!-)' . $keyword . '(?!-))/i', str_replace('%string%', '\1', $this->_wrapper), $value);
}
From this keyword list:
- lorem
- ipsum
- sit amet
- null
- sed
- sed enim
I'd like to result in:
-Lorem- -ipsum- dolor -sit amet-, consectetur adipiscing elit. Phasellus rhoncus venenatis orci sed porta. Sed non dolor eros. Suspendisse a massa -sit amet- nulla egestas facilisis. Cras fringilla, leo ac ullamcorper semper, urna eros pretium lectus, nec rhoncus ligula risus eu velit. Nulla eu dapibus magna. Sed vehicula tristique lacinia. Maecenas tincidunt metus at urna consequat nec congue libero iaculis. Nulla facilisi. Phasellus -sed- sem ut risus mattis accumsan eu -sed enim-. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse id est velit, eu cursus quam. Vivamus lacinia euismod pretium.
Any ideas?