Skip to content

Instantly share code, notes, and snippets.

@frasten
Created March 24, 2010 21:12
Show Gist options
  • Save frasten/342824 to your computer and use it in GitHub Desktop.
Save frasten/342824 to your computer and use it in GitHub Desktop.
diff --git a/smilies-package.inc.php b/smilies-package.inc.php
index bb5f42c..62c40d6 100644
--- a/smilies-package.inc.php
+++ b/smilies-package.inc.php
@@ -8,7 +8,7 @@ class smilies_package
var $url_path;
var $smilies = array();
- var $smiliessearch = array();
+ var $smiliessearch = '';
var $smiliesreplace = array();
var $author;
@@ -41,16 +41,35 @@ class smilies_package
$this->smilies = $wp_smilies;
- foreach ( (array) $this->smilies as $smiley => $img ) {
- $this->smiliessearch[] = '/(\s|^)'.preg_quote($smiley, '/').'(\s|$)/';
- $smiley_masked = htmlspecialchars(trim($smiley), ENT_QUOTES);
- $this->smiliesreplace[] = " <img src='$this->url_path/$img' alt='$smiley_masked' class='wp-smiley' /> ";
+ /* Let's reorder the smilies from the longest to the shortest, so
+ * that they are matched in different priorities.
+ * For example: if I have two smilies :o) and :o
+ * if someone writes in a comment :o) it should match the full one,
+ * rather the shorter one.
+ */
+ $ordered_smilies = $this->smilies;
+ uksort($ordered_smilies, array(&$this, 'sort_smilies_by_size'));
+
+ $regexp_chunks = array();
+ foreach ( (array) $ordered_smilies as $smiley => $img ) {
+ $smiley = trim($smiley);
+ $chunk = '(?:' . preg_quote($smiley, '/') . ')';
+ $regexp_chunks[] = $chunk;
+ $smiley_masked = htmlspecialchars($smiley, ENT_QUOTES);
+ $this->smiliesreplace[$smiley] = " <img src='$this->url_path/$img' alt='$smiley_masked' class='wp-smiley' /> ";
}
+ // Merge all the regexps into one.
+ $this->smiliessearch = "/(?<=\s|^)(?:" . implode('|', $regexp_chunks) . ")(?=\s|$)/m";
wp_cache_add($package, get_object_vars($this), 'smilies');
}
}
-
+
+ function sort_smilies_by_size($a, $b) {
+ if (strlen($a) == strlen($b)) return 0;
+ return (strlen($a) > strlen($b)) ? -1 : 1;
+ }
+
function get_package_data() {
$package_data = implode('', file($this->path . k_SMILIES_CONFIG_FILE));
@@ -163,4 +182,4 @@ class smilies_package
}
-?>
\ No newline at end of file
+?>
diff --git a/smilies-themer.php b/smilies-themer.php
index d7fe1dd..045b1fa 100644
--- a/smilies-themer.php
+++ b/smilies-themer.php
@@ -92,7 +92,7 @@ class smilies_themer
for ($i = 0; $i < $stop; $i++) {
$content = $textarr[$i];
if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag
- $content = preg_replace($this->smilies->smiliessearch, $this->smilies->smiliesreplace, $content);
+ $content = preg_replace_callback($this->smilies->smiliessearch, array(&$this, 'translate_smiley'), $content);
}
$output .= $content;
}
@@ -103,6 +103,15 @@ class smilies_themer
return $output;
}
+ function translate_smiley($smiley) {
+ if (count($smiley) == 0) {
+ return '';
+ }
+
+ $smiley = trim(reset($smiley));
+ return $this->smilies->smiliesreplace[$smiley];
+ }
+
function activate() {
@@ -212,4 +221,4 @@ class smilies_themer
$smilies_themer =& new smilies_themer;
-?>
\ No newline at end of file
+?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment