Skip to content

Commit

Permalink
New highlighting function that is able to cope with HTML.
Browse files Browse the repository at this point in the history
It's a bit slower, but links in forums no longer cause problems.

See bug #589
  • Loading branch information
moodler committed Aug 9, 2003
1 parent 33428ff commit 88438a5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 15 deletions.
55 changes: 50 additions & 5 deletions lib/weblib.php
Expand Up @@ -619,15 +619,61 @@ function convert_urls_into_links(&$text) {


/// Make lone URLs into links. eg http://moodle.com/ /// Make lone URLs into links. eg http://moodle.com/
$text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", $text = eregi_replace("([[:space:]]|^|\(|\[)([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
"\\1<a href=\"\\2://\\3\\4\" TARGET=\"newpage\">\\2://\\3\\4</a>", $text); "\\1<a href=\"\\2://\\3\\4\" target=\"newpage\">\\2://\\3\\4</a>", $text);


/// eg www.moodle.com /// eg www.moodle.com
$text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])", $text = eregi_replace("([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?/&=])",
"\\1<a href=\"http://www.\\2\\3\" TARGET=\"newpage\">www.\\2\\3</a>", $text); "\\1<a href=\"http://www.\\2\\3\" target=\"newpage\">www.\\2\\3</a>", $text);
} }


function highlight($needle, $haystack) { function highlight($needle, $haystack, $case=0,
$left_string="<span class=\"highlight\">", $right_string="</span>") {
/// 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<sizeof($list_array); $i++) {
if (strlen($list_array[$i]) == 1) {
$list_array[$i] = "";
}
}
$list_of_words = implode(" ", $list_array);
$list_of_words_cp = $list_of_words;
$final = array();
preg_match_all('/<(.+?)>/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 /// 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)); $parts = explode(strtolower($needle), strtolower($haystack));


Expand All @@ -637,15 +683,14 @@ function highlight($needle, $haystack) {
$parts[$key] = substr($haystack, $pos, strlen($part)); $parts[$key] = substr($haystack, $pos, strlen($part));
$pos += strlen($part); $pos += strlen($part);


$parts[$key] .= "<SPAN CLASS=highlight>".substr($haystack, $pos, strlen($needle))."</SPAN>"; $parts[$key] .= "<span class=\"highlight\">".substr($haystack, $pos, strlen($needle))."</span>";
$pos += strlen($needle); $pos += strlen($needle);
} }


return (join('', $parts)); return (join('', $parts));
} }





/// STANDARD WEB PAGE PARTS /////////////////////////////////////////////////// /// STANDARD WEB PAGE PARTS ///////////////////////////////////////////////////


function print_header ($title="", $heading="", $navigation="", $focus="", $meta="", $cache=true, $button="&nbsp;", $menu="") { function print_header ($title="", $heading="", $navigation="", $focus="", $meta="", $cache=true, $button="&nbsp;", $menu="") {
Expand Down
8 changes: 6 additions & 2 deletions mod/forum/lib.php
Expand Up @@ -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; global $THEME, $USER, $CFG;


echo "<a name=\"$post->id\"></a>"; echo "<a name=\"$post->id\"></a>";
Expand Down Expand Up @@ -959,7 +959,11 @@ function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link
echo "</a> (".get_string("numwords", "", $numwords).")...</p>"; echo "</a> (".get_string("numwords", "", $numwords).")...</p>";
} else { } else {
// Print whole message // 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; echo $attachedimages;
} }


Expand Down
15 changes: 7 additions & 8 deletions mod/forum/search.php
Expand Up @@ -51,22 +51,21 @@
error("Could not find forum $discussion->forum"); error("Could not find forum $discussion->forum");
} }


$post->subject = highlight("$search", $post->subject); $post->subject = highlightfast("$search", $post->subject);
$discussion->name = highlight("$search", $discussion->name); $discussion->name = highlightfast("$search", $discussion->name);


$fullsubject = "<A HREF=\"view.php?f=$forum->id\">$forum->name</A>"; $fullsubject = "<a href=\"view.php?f=$forum->id\">$forum->name</a>";
if ($forum->type != "single") { if ($forum->type != "single") {
$fullsubject .= " -> <A HREF=\"discuss.php?d=$discussion->id\">$discussion->name</A>"; $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">$discussion->name</a>";
if ($post->parent != 0) { if ($post->parent != 0) {
$fullsubject .= " -> <A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</A>"; $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</a>";
} }
} }


$post->subject = $fullsubject; $post->subject = $fullsubject;
$post->message = highlight("$search", $post->message);


$fulllink = "<P ALIGN=right><A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">".get_string("postincontext", "forum")."</A></P>"; $fulllink = "<p align=\"right\"><a href=\"discuss.php?d=$post->discussion&parent=$post->id\">".get_string("postincontext", "forum")."</a></p>";
forum_print_post($post, $course->id, false, false, false, false, $fulllink); forum_print_post($post, $course->id, false, false, false, false, $fulllink, $search);


echo "<BR>"; echo "<BR>";
} }
Expand Down

0 comments on commit 88438a5

Please sign in to comment.