#2886 closed enhancement (worksforme)
Smilies don't get regognized properly, and they get substituted twice
| Reported by: | Anoyomouse | Owned by: | leftjustified |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Administration | Version: | 2.0.3 |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: |
Description
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.
Attachments (2)
Change History (8)
#2
@
20 years ago
- Keywords bg|has-patch bg|commit added
- Milestone → 2.0.4
- Owner changed from to
- Status new → assigned
functions-formatting.php.diff = 2.0 branch
formatting.php.diff = 2.1 branch
#3
@
20 years ago
- Keywords has-patch commit added; bg|has-patch bg|commit removed
- Milestone 2.0.4 → 2.2
#4
@
20 years ago
- Keywords needs-patch added; has-patch commit removed
Patch needs a refresh.
If this even still is an issue after the recent smilie changes and static sorting?
We did a static sort of the smilies in #2778. Sorting on the fly was too slow. Being slowed down for smilies makes me frowny. :-)