I’m replacing some text using a regex that I’ve found here.
$items = array(
':)' => 'smile',
':(' => 'sad',
'=))' => 'laugh',
':p' => 'tongue',
);
foreach($items as $key => $class)
$regex[] = preg_quote($key, '#');
$regex = '#(?!<\w)('.implode('|', $regex).')(?!\w)#';
$string = preg_replace_callback($regex, function($matches) use($items){
if(isset($items[$matches[0]]))
return '<span class="'.$items[$matches[0]].'">'.$matches[0].'</span>';
return $matches[0];
}, $string);
It works but how can ignore matches within HTML tag definitions (like within tag attributes) ?
For example:
$string = 'Hello :) <a title="Hello :)"> Bye :( </a>';
=> The second :) should be ignored.
Here’s a
DOMDocument-based implementation that does a by-the-book string replacement for your HTML:Wrap it in a function and you’re good to go. It may be more lines of code than regex, but it’s not an ugly, bug-ridden maintenance nightmare (like any regex-based solution would be).