[PHP] highlighting multi term search results
hi, i'm working on a function that highlights search results. problem: a search input like "te est"; two terms that match one word ("test"). the mysql query matches entries like this but my function isn't able to highlight them the right way: function highlight($src_terms, $src_terms_int, $result) { $i = 0; while ($i < $src_terms_int) { $result = preg_replace('/('.$src_terms[$i].')/si', ''.$src_terms[$i].'', $result); $i++; } return $result; } $search = "te est"; // user input to search for $src_terms = explode(" ", $search); $src_terms_int = count($src_terms); $result = "this is just a test"; // result from database print highlight($src_terms, $src_terms_int, $result); ?> output: this is just a test (after the first term is highlighted the second one can't be found anymore.) someone has an idea how to work around this? thanks for your effort! jonas ps: please also let me know if you know of a website that might help, i didn't find anything useful. thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: highlighting multi term search results
Am Sonntag, 07.09.03 um 14:11 Uhr schrieb Catalin Trifu <[EMAIL PROTECTED]>: Here the $result is changed to 'test' on the first search. Obviously on the second replace the term will not be found anymore! yes, it's obvious. like i said: output: this is just a test (after the first term is highlighted the second one can't be found anymore.) but how to work around it?? - jns /snip/ hi, i'm working on a function that highlights search results. problem: a search input like "te est"; two terms that match one word ("test"). the mysql query matches entries like this but my function isn't able to highlight them the right way: function highlight($src_terms, $src_terms_int, $result) { $i = 0; while ($i < $src_terms_int) { $result = preg_replace('/('.$src_terms[$i].')/si', ''.$src_terms[$i].'', $result); $i++; } return $result; } $search = "te est"; // user input to search for $src_terms = explode(" ", $search); $src_terms_int = count($src_terms); $result = "this is just a test"; // result from database print highlight($src_terms, $src_terms_int, $result); ?> output: this is just a test (after the first term is highlighted the second one can't be found anymore.) someone has an idea how to work around this? thanks for your effort! jonas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: highlighting multi term search results
Am Sonntag, 07.09.03 um 21:17 Uhr schrieb John W. Holmes: output: this is just a test (after the first term is highlighted the second one can't be found anymore.) but how to work around it?? Highlight the longest words first? I don't think that would change anything. A search for "te est" then highlights "test" into "test" and "te" can't be found anymore. thanks anyway!! j snip 8< function highlight($src_terms, $src_terms_int, $result) { $i = 0; while ($i < $src_terms_int) { $result = preg_replace('/('.$src_terms[$i].')/si', ''.$src_terms[$i].'', $result); $i++; } return $result; } $search = "te est"; // user input to search for $src_terms = explode(" ", $search); $src_terms_int = count($src_terms); $result = "this is just a test"; // result from database print highlight($src_terms, $src_terms_int, $result); ?> output: this is just a test (after the first term is highlighted the second one can't be found anymore.) someone has an idea how to work around this? thanks for your effort! jonas ps: please also let me know if you know of a website that might help, i didn't find anything useful. thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: highlighting multi term search results
Am Montag, 08.09.03 um 04:21 Uhr schrieb Lee O'Mara: Why not just allow for bold tags in the search term? What I mean is, I think you can get the results you want by allowing any number of open or close bold tags between each letter of the search term. i thought so too (but i had no idea how to code it); this must be a solution. i've tested your function like this: function highlight($src_terms, $src_terms_int, $result) { $i = 0; while ($i < $src_terms_int) { $termWithOptionalBold = preg_replace('/(.)/', '(<\/?b>) *\1',$src_terms[$i]); $result = preg_replace('/(<\/?b>)*('.$termWithOptionalBold.')(<\/?b>) */si', '\2', $result); $i++; } return $result; } $search = "te est"; // user input to search for $src_terms = explode(" ", $search); $src_terms_int = count($src_terms); $result = "this is just a test"; // result from database print highlight($src_terms, $src_terms_int, $result); ?> but it didn't highlight anything. btw, the bold tag in this example is used to simplify it and stands for something like what makes it even trickier, i guess. thanks for your help! jns -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regex/preg_replace() difficulty
Regular Expressions: How can I indicate that the contents of a term (user input*) needs to be treated as 'non-operators/control characters' (as *word* to match in that exact way)? (* Because the term is a user's input I can't escape the control characters manually.) Example: $result = preg_replace('/('.$termWithOptionalBold.')(<\/?span[^>]*>)*()/si', '\1', $result); [If $termWithOptionalBold is a "." (period) for example, any char will be matched--instead of only the "."] Any suggestions? Thanks a lot for your effort! Best wishes, jonas PS: Somewhere I read that '\Q' would do something like that but it didn't work. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] (another) regex difficulty
regular expressions the example below should turn any character exept "\*" (*= any char) into an "x": $term = preg_replace('/[^(\\\)+(.){1}]/', 'x', $term); but i guess because of the [brackets] the "." (period) is treated as character "." instead as metacharacter (that matches any char). anyone knows how to work around this? thanks for your effort best wishes, jns ps: thanks for your help, holmes and kilimajer! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] (another) regex difficulty
01.10.03 at 18:17 Curt Zirzow wrote: preg_replace('/(?01.10.03 at 18:27 CPT John W. Holmes wrote: $term = preg_replace('/[^\\]./','x',$term); they don't work (thanks anyway) it's pretty simple: i need a regex that matches any character in a string except "\*" (* stands for any char that follows the "\"). example: "this is \ba test" should match: this a is test isn't there a way to do this? best wishes, jns -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regex drive me crazy
1. The PHP manual sais to escape the escape char, it has to be written twice*, but: $term = preg_replace('/(\\)/', 'backslash $1', $term); causes an error** while using three backslashes (see 2.) works. 2.1. $term = "beg \ end"; print preg_replace('/(\\\)/', 'backslash $1', $term); returns: beg backslash \ end 2.2. $term = "beg \\ end"; print preg_replace('/(\\\)/', 'backslash $1', $term); returns: beg backslash \ end (the same as 2.1.) 2.3. $term = "beg \\\ end"; print preg_replace('/(\\\)/', 'backslash $1', $term); returns: beg backslash \backslash \ end Does this make ANY sense? - jns * http://www.php.net/manual/en/pcre.pattern.syntax.php ** "Warning: Compilation failed: missing ) at offset 3 in /home/jonas/public_html/test01.php on line 3" Am Mittwoch, 01.10.03 um 19:14 Uhr schrieb CPT John W. Holmes: Regular expressions are for "pattern matching"... not matching everything BUT a pattern. good point! ;) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regex drive me crazy
Am Mittwoch, 01.10.03 um 22:27 Uhr schrieb Chris W. Parker: Seeing as how you haven't had a response yet, I'll ask this: what exactly is your question? I'm not sure as to what it is that you want. My question are: a) Why does the PHP manual say that backslashes get escaped by writing them twice while this causes an error in my example (see below)? b) Why does a different input (2.1. and 2.2.) result in the same output (in the example I enclosed)? c) Why do I hate regular expressions so much? Choose one. - Jns 8< 1. The PHP manual sais to escape the escape char, it has to be written twice*, but: $term = preg_replace('/(\\)/', 'backslash $1', $term); causes an error** while using three backslashes (see 2.) works. 2.1. $term = "beg \ end"; print preg_replace('/(\\\)/', 'backslash $1', $term); returns: beg backslash \ end 2.2. $term = "beg \\ end"; print preg_replace('/(\\\)/', 'backslash $1', $term); returns: beg backslash \ end (the same as 2.1.) 2.3. $term = "beg \\\ end"; print preg_replace('/(\\\)/', 'backslash $1', $term); returns: beg backslash \backslash \ end * http://www.php.net/manual/en/pcre.pattern.syntax.php ** "Warning: Compilation failed: missing ) at offset 3 in /home/jonas/public_html/test01.php on line 3" -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] (another) regex difficulty
Am Donnerstag, 02.10.03 um 01:37 Uhr schrieb Cristian Lavaque: doh, I now understand your question, you don't mean the asterisk literally, but as any character that follows a backslash... sorry -_- that's right ;) -- anyway, thanks for your effort! my problem was that i misunderstood the escape-the-meta-char-idea (see below). Am Mittwoch, 01.10.03 um 23:23 Uhr schrieb Curt Zirzow: '\\' always gets translated to '\' So the string: '/(\\)/' translates to '/(\)/' which is an invalid regex, you'll get a complaint about unclosed paren. this is also a bit tricky: echo "beg \\ end\n"; echo "beg \ end\n"; c) Why do I hate regular expressions so much? perhaps because you're using them wrong. klugscheisser ;) I've answered all 5 of em so far. thanks, your answers helped a lot! - jns -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php