[wp-trac] [WordPress Trac] #2886: Smilies don't get regognized

WordPress Trac wp-trac at lists.automattic.com
Fri Jun 30 19:10:15 GMT 2006


#2886: Smilies don't get regognized properly, and they get substituted twice
----------------------------+-----------------------------------------------
 Reporter:  Anoyomouse      |       Owner:  anonymous
     Type:  enhancement     |      Status:  new      
 Priority:  normal          |   Milestone:           
Component:  Administration  |     Version:  2.0.3    
 Severity:  normal          |    Keywords:           
----------------------------+-----------------------------------------------
 The smilie replacement function has a problem that it can replace already-
 replaced strings because the str_replace function with array arguments
 does things linierally, this function (found in the php documentation
 under str_replace) makes this atomic, so replacements can't affect already
 replaced strings (see lots of comments in php docs for str_replace)


 attached is a patch to fix this error, as well as to sort the smilie list
 from biggest smily to smallest, so replacement "should" occour correctly


 this will patch the functions-formatted.php in the wp-includes directory

 --- functions-formatting-old.php        2006-05-26 15:27:00.000000000
 +0000
 +++ functions-formatting-new.php        2006-06-26 13:34:54.000000000
 +0000
  -583,6 +583,45 @@
         return $text;
  }

 +function rev_len_cmp($a, $b)
 +{
 +       if (strlen($a) == strlen($b)) {
 +               if ($a == $b)
 +                       return 0;
 +               return $a < $b;
 +       }
 +       return (strlen($a) < strlen($b)) ? -1 : 1;
 +}
 +
 +/**
 + * same as str_replace (array, array, string), but changing only the text
 in the
 + * original string
 + * $search and $replace are arrays of strings, $subject is a string
 + */
 +function str_replace_clean($search, $replace, $subject) {
 +   if (!is_array($search) or !is_array($replace) or !is_string($subject))
 return $subject;
 +   while (count($search)) {
 +       // get current terms, reduce the arrays for the next iteration
 +       $search_text = array_shift($search);
 +       $replace_text = array_shift($replace);
 +       // check if the substring is present
 +       $pos = strpos($subject, $search_text);
 +       if (is_int($pos)) {
 +           // match found - break in pieces
 +           $pieces = explode($search_text, $subject);
 +           if (count($search)) { // only if there are more substitutions
 to do
 +               // make next substitutions in every piece of text between
 matches
 +               foreach ($pieces as $k => $v) {
 +                   if (strlen($v)) $pieces[$k] =
 str_replace_clean($search, $replace, $v);
 +               }
 +           }
 +           $subject = join($replace_text, $pieces);
 +           break;
 +       }
 +   }
 +   return $subject;
 +}
 +
  function convert_smilies($text) {
         global $wp_smiliessearch, $wp_smiliesreplace;
      $output = '';
  -590,12 +629,17 @@
                 // HTML loop taken from texturize function, could possible
 be consolidated
                 $textarr = preg_split("/(<.*>)/U", $text, -1,
 PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
                 $stop = count($textarr);// loop stuff
 +
 +               $smil_ary = array_combine($wp_smiliessearch,
 $wp_smiliesreplace);
 +               uksort($smil_ary, "rev_len_cmp");
 +               $smil_ary = array_reverse($smil_ary);
                 for ($i = 0; $i < $stop; $i++) {
                         $content = $textarr[$i];
                         if ((strlen($content) > 0) && ('<' !=
 $content{0})) { // If it's not a tag
 -                               $content = str_replace($wp_smiliessearch,
 $wp_smiliesreplace, $content);
 +                               $content =
 str_replace_clean(array_keys($smil_ary), array_values($smil_ary),
 $content, $count);
                         }
                         $output .= $content;
 +                       $content = '';
                 }
         } else {
                 // return default text.

-- 
Ticket URL: <http://trac.wordpress.org/ticket/2886>
WordPress Trac <http://wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list