in reply to Highlighting a text inside another one.
$message = 'I\'d like to kick Bill Gates\' ass'; $page = 'http://www.microsoft.com'; highlight_letters($message, $page);
Then you surround the relevant letters with tags that make them blink red when the user moves the mouse. Or whatever.
You've got two choices. Use a series of simple regexes, and loop through the text looking for a word at a time, or use one huge gnarly regex. I suspect the latter is the way to go. The reason is that you need to do things like backtracking - suppose you get to the end of the text, you've found whole words for all of your sentence but then you can't find the last word... you need to backtrack, try using letter matches in some place, and hope that you'll win yourself more space to find the last word. E.g.
$message = "foo foo"; $page_text = "f o o foo"; # whole word matching failed! We need to let +ter-match the first foo!
Now the regex engine has this stuff built in.
To build the regex... phew. I think you'll need a lot of lookahead and lookbehind expressions, and experimental features (see perlre). The logic is gonna be recursive, and looks like this:
Sentence S "matches" page P if
(the first word W of S has a match at position M in P or
W's letters have matches in P, where M is the last matching letter)
and S' (S minus its first word) "matches" P' (the portion of P after M)
Doing this is hellishly tricky with one big regex, but may save you a lot of extra coding to implement your own backtracking logic. You'll certainly be a regex whizz by the end.
dave hj~
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Highlighting a text inside another one.
by TomSW (Novice) on Feb 28, 2002 at 09:53 UTC |