[geeklog-cvs] geeklog: Highlighting of whole words, provided by lwc (part of b...
geeklog-cvs at lists.geeklog.net
geeklog-cvs at lists.geeklog.net
Mon May 25 13:25:15 EDT 2009
details: http://project.geeklog.net/cgi-bin/hgweb.cgi/rev/3ff537b94940
changeset: 7057:3ff537b94940
user: Sami Barakat
date: Mon May 25 18:23:19 2009 +0100
description:
Highlighting of whole words, provided by lwc (part of bug #0000874)
diffstat:
public_html/lib-common.php | 48 ++++++++++++++++++++++--------------------------
1 files changed, 22 insertions(+), 26 deletions(-)
diffs (58 lines):
diff -r c68402fc8a20 -r 3ff537b94940 public_html/lib-common.php
--- a/public_html/lib-common.php Mon May 25 16:57:48 2009 +0100
+++ b/public_html/lib-common.php Mon May 25 18:23:19 2009 +0100
@@ -5806,32 +5806,28 @@
*/
function COM_highlightQuery( $text, $query, $class = 'highlight' )
{
- $query = str_replace( '+', ' ', $query );
-
- // escape all the other PCRE special characters
- $query = preg_quote( $query );
-
- // ugly workaround:
- // Using the /e modifier in preg_replace will cause all double quotes to
- // be returned as \" - so we replace all \" in the result with unescaped
- // double quotes. Any actual \" in the original text therefore have to be
- // turned into \\" first ...
- $text = str_replace( '\\"', '\\\\"', $text );
-
- $mywords = explode( ' ', $query );
- foreach( $mywords as $searchword )
- {
- if( !empty( $searchword ))
- {
- $searchword = preg_quote( str_replace( "'", "\'", $searchword ));
- $searchword = str_replace('/', '\\/', $searchword);
- $text = preg_replace( '/(\>(((?>[^><]+)|(?R))*)\<)/ie', "preg_replace('/(?>$searchword+)/i','<span class=\"$class\">\\\\0</span>','\\0')", '<!-- x -->' . $text . '<!-- x -->' );
- }
- }
-
- // ugly workaround, part 2
- $text = str_replace( '\\"', '"', $text );
-
+ // escape PCRE special characters
+ $query = preg_quote($query, '/');
+
+ $mywords = explode(' ', $query);
+ foreach ($mywords as $searchword)
+ {
+ if (!empty($searchword))
+ {
+ $before = "/(?!(?:[^<]+>|[^>]+<\/a>))\b";
+ $after = "\b/i";
+ if ($searchword <> utf8_encode($searchword)) {
+ if (@preg_match('/^\pL$/u', urldecode('%C3%B1'))) { // Unicode property support
+ $before = "/(?<!\p{L})";
+ $after = "(?!\p{L})/u";
+ } else {
+ $before = "/";
+ $after = "/u";
+ }
+ }
+ $text = preg_replace($before . $searchword . $after, "<span class=\"$class\">\\0</span>", '<!-- x -->' . $text . '<!-- x -->' );
+ }
+ }
return $text;
}
More information about the geeklog-cvs
mailing list