With the code below some smilies won’t work (the text isn’t replaced by a smiley). When I change the order of the $smileys array, the broken smileys work but others don’t.
So I know the order of the $smileys array determines the replacement of the smilies, but I don’t know why.
Can anybody help me to let all the smileys work properly? Thanks
public $smileys = array(
'tongetje.gif' => array(':p',':P'),
'cool.gif' => array(':8'),
'dansen.gif'=> array('(dance)'),
'mondje_dicht.gif' => array(':|'),
'muur_hoofd.gif' => array('|:('),
'huilen.gif' => array('(h)'),
'loser.gif' => array('(:)'),
'lachje.gif' => array(':)',':-)'),
'wink.gif' => array(';)'),
'lach.gif' => array(':d',':-D'),
'lollol.gif' => array(':#'),
'bloos.gif' => array('(b)'),
'stom.gif' => array(':s'),
'doei.gif' => array('(d)'),
'engeltje.gif' => array('O-)'),
'boze_smiley.gif' => array(':('),
'1april.gif' => array(':1)'),
'koning.gif' => array('(koning)'),
'offtopic.gif' => array('*-*'),
'opgeven_verlegen.gif' => array('O+'),
'piraat.gif' => array(':F'),
'politie.gif' => array('(p)'),
'reggae.gif' => array('(r)'),
'jammie.gif' => array(':9'),
'schamen.gif' => array(':-8'),
'verliefd.gif' => array(':00'),
'middelvinger.gif' => array(':--')
);
private function ubb_smileys($string) {
$counter = 1;
foreach($this->smileys as $imagename => $imagetags) {
if(is_numeric($this->smilies_enable)) {
if($counter <= $this->smilies_enable) {
foreach($imagetags as $int => $tag) {
$tag = str_replace("\\", "", $tag);
$src = UBB_SMILEYS_PATH.$imagename;
$image = UBB_SMILEYS_STRING;
$image = str_replace('${TAG}', $tag, $image);
$image = str_replace('${SRC}', $src, $image);
$string = str_replace($tag, $image, $string);
}
}
} else {
foreach($imagetags as $int => $tag) {
$tag = str_replace("\\", "", $tag);
$src = UBB_SMILEYS_PATH.$imagename;
$image = UBB_SMILEYS_STRING;
$image = str_replace('${TAG}', $tag, $image);
$image = str_replace('${SRC}', $src, $image);
$string = str_replace($tag, $image, $string);
}
}
$counter++;
}
return $string;
}
I expect it is because some smileys are subsets of others. For example ‘:)’ is a subset of ‘(:)’. So if it came before ‘:)’, ‘(:)’ would never be found.
Include the most complicated ones first, and then any smiley that is a subset of another smiley must come later.
This explains why the ordering is affecting your results.
That said — I can’t find a specific example of the ordering in the order you posted above (although I may not have looked for long enough). With the above-posted order, which smileys are not working for you?
EDIT:
I tested with the below. Works fine for me. What are you using as a test string?