I was looking at the code of smileys module and the way for smileys replacing is wors. Its too slow and not absolutely 100% working.
I with my friend wrote smileys replacing system for our old web, witch uses better technique.
I like to share with you Gurpartap Singh.
In DB is 2 tables - one for smiley image and second for smiley codes (each code is one record).
Than from tables are selected codes and replacing images into array sort by lenght of smiley code.
At the first wave are smileys code replaced by textual codes.
At the second wave are textual codes replaced by images - this was write for control of ammount replaced smileys.
Here is code preview:
function nahradit_smajly($text){
global $UZIVATEL;
// nepreje si nahrazova smajly
if ($UZIVATEL[nahrazovat_smajly]==0) return $text;
if (!$this->inicializace) $this->init();
//$text = ":))text:))".$text;
if ($this->pocet_smajlu > 0){
global $smajly_kod_format,$smajly_kod,$smajly_repl_format,$smajly_repl_img;
global $nahradil_pocet;
// prevest na format ###SMAJL$iEND###
$text = preg_replace($smajly_kod_format, $smajly_repl_format, $text);
// vykonat nahrazeni smajlu (s hlidanim poctu)
$nahradil_pocet = 0;
$text = preg_replace("/###SMAJL([0-9]{1,4})END###/e", "special_nahrad_smajla('$1','$this->max')", $text);
$this->nahradil = $nahradil_pocet;
$this->nahradil_celkem += $nahradil_pocet;
}
return $text;
} //end nahradit
function special_nahrad_smajla($smajl_index,$max){
global $smajly_kod,$smajly_repl_img;
global $nahradil_pocet;
if (($nahradil_pocet < $max)||($max < 0)){
$smajl = $smajly_repl_img[$smajl_index];
$nahradil_pocet++;
}else{ // jiz nepovadet nahrazeni
$smajl = $smajly_kod[$smajl_index];
}
return $smajl;
}
There is no included function for DB selecting, but it is only for preview.
If you will be interested for this, I can provide more info.
I would write it my own, but I don't have much time :(
Comments
Comment #1
avpadernoIf it would be translated in English it would be more understandable (name of variables, and comments).
Like it is, I can barely understand what it is doing.
Comment #2
avpadernoThe code is also incomplete. It uses the variable
$this, but I don't see any class declaration, nor a call tonew().Comment #3
stokito commentedHm... I am don't understand your code, but something I am understand.
1. As you want, we don't need additional table for smiles acronyms. Acronyms are exploded from string, and this is best and faster way.
2.
This idea is not stupid (but is not fuster).
When we replace smiley whith
< img>tag, his attributes such asalt(acronym) andtitle(smiley description) also can contain smiley acronym!See #299401: Acronyms in description also replaced whith img tag bug