From 88438a58934acd1b59fe55d5653eecdf1e036db0 Mon Sep 17 00:00:00 2001 From: moodler Date: Sat, 9 Aug 2003 03:57:46 +0000 Subject: [PATCH] New highlighting function that is able to cope with HTML. It's a bit slower, but links in forums no longer cause problems. See bug #589 --- lib/weblib.php | 55 ++++++++++++++++++++++++++++++++++++++++---- mod/forum/lib.php | 8 +++++-- mod/forum/search.php | 15 ++++++------ 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/lib/weblib.php b/lib/weblib.php index 5bed9f2544483..db95bfc8672d9 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -619,15 +619,61 @@ function convert_urls_into_links(&$text) { /// Make lone URLs into links. eg http://moodle.com/ $text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", - "\\1\\2://\\3\\4", $text); + "\\1\\2://\\3\\4", $text); /// eg www.moodle.com $text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])", - "\\1www.\\2\\3", $text); + "\\1www.\\2\\3", $text); } -function highlight($needle, $haystack) { +function highlight($needle, $haystack, $case=0, + $left_string="", $right_string="") { +/// This function will highlight search words in a given string +/// It cares about HTML and will not ruin links. It's best to use +/// this function after performing any conversions to HTML. +/// Function found here: http://forums.devshed.com/t67822/scdaa2d1c3d4bacb4671d075ad41f0854.html + + $list_of_words = eregi_replace("[^-a-zA-Z0-9&']", " ", $needle); + $list_array = explode(" ", $list_of_words); + for ($i=0; $i/is',$haystack,$list_of_words); + + foreach (array_unique($list_of_words[0]) as $key=>$value) { + $final['<|'.$key.'|>'] = $value; + } + + $haystack = str_replace($final,array_keys($final),$haystack); + $list_of_words_cp = eregi_replace(" +", "|", $list_of_words_cp); + + if ($list_of_words_cp{0}=="|") { + $list_of_words_cp{0} = ""; + } + if ($list_of_words_cp{strlen($list_of_words_cp)-1}=="|") { + $list_of_words_cp{strlen($list_of_words_cp)-1}=""; + } + $list_of_words_cp = "(".trim($list_of_words_cp).")"; + + if (!$case){ + $haystack = eregi_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack); + } else { + $haystack = ereg_replace("$list_of_words_cp", "$left_string"."\\1"."$right_string", $haystack); + } + $haystack = str_replace(array_keys($final),$final,$haystack); + + return stripslashes($haystack); +} + +function highlightfast($needle, $haystack) { /// This function will highlight instances of $needle in $haystack +/// It's faster that the above function and doesn't care about +/// HTML or anything. $parts = explode(strtolower($needle), strtolower($haystack)); @@ -637,7 +683,7 @@ function highlight($needle, $haystack) { $parts[$key] = substr($haystack, $pos, strlen($part)); $pos += strlen($part); - $parts[$key] .= "".substr($haystack, $pos, strlen($needle)).""; + $parts[$key] .= "".substr($haystack, $pos, strlen($needle)).""; $pos += strlen($needle); } @@ -645,7 +691,6 @@ function highlight($needle, $haystack) { } - /// STANDARD WEB PAGE PARTS /////////////////////////////////////////////////// function print_header ($title="", $heading="", $navigation="", $focus="", $meta="", $cache=true, $button=" ", $menu="") { diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 015087f243041..c2a2984054339 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -910,7 +910,7 @@ function forum_make_mail_post(&$post, $user, $touser, $course, } -function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") { +function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="", $highlight="") { global $THEME, $USER, $CFG; echo "id\">"; @@ -959,7 +959,11 @@ function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link echo " (".get_string("numwords", "", $numwords).")...

"; } else { // Print whole message - echo format_text($post->message, $post->format); + if ($highlight) { + echo highlight($highlight, format_text($post->message, $post->format)); + } else { + echo format_text($post->message, $post->format); + } echo $attachedimages; } diff --git a/mod/forum/search.php b/mod/forum/search.php index 27dffd54a059d..3218d5805beff 100644 --- a/mod/forum/search.php +++ b/mod/forum/search.php @@ -51,22 +51,21 @@ error("Could not find forum $discussion->forum"); } - $post->subject = highlight("$search", $post->subject); - $discussion->name = highlight("$search", $discussion->name); + $post->subject = highlightfast("$search", $post->subject); + $discussion->name = highlightfast("$search", $discussion->name); - $fullsubject = "id\">$forum->name"; + $fullsubject = "id\">$forum->name"; if ($forum->type != "single") { - $fullsubject .= " -> id\">$discussion->name"; + $fullsubject .= " -> id\">$discussion->name"; if ($post->parent != 0) { - $fullsubject .= " -> discussion&parent=$post->id\">$post->subject"; + $fullsubject .= " -> discussion&parent=$post->id\">$post->subject"; } } $post->subject = $fullsubject; - $post->message = highlight("$search", $post->message); - $fulllink = "

discussion&parent=$post->id\">".get_string("postincontext", "forum")."

"; - forum_print_post($post, $course->id, false, false, false, false, $fulllink); + $fulllink = "

discussion&parent=$post->id\">".get_string("postincontext", "forum")."

"; + forum_print_post($post, $course->id, false, false, false, false, $fulllink, $search); echo "
"; }