in reply to Text highlighting (not language syntax)

Hum, not easy. You need a parser. This one should do the trick if you're starting from text.
use HTML::Entities qw( encode_entities ); sub text_to_html { my $terms = $_[1]; my ($terms_pat) = map "(?:$_)", join '|', map quotemeta, @$terms; ( my $html = $_[0] ) =~ s{ ( (?: (?!$terms_pat). )* ) ( $terms_pat+ ) | ( .+ ) }{ defined($1) ? encode_entities("$1") . "<b>" . encode_entities("$2") . "</b>" : encode_entities("$3") }xseg; return $html; } print text_to_html("best car wash & repair!!", [qw( car repair )]);

Starting from HTML is much trickier.

Update: Simplified code.
Update: Fixed s// </b> / problem mentioned in a reply.

Replies are listed 'Best First'.
Re^2: Text highlighting (not language syntax)
by vit (Friar) on Jan 22, 2010 at 23:09 UTC
    Thanks, I am starting from text. I tested and got
    Undefined subroutine &main::escaped_entities called at test_highlight. +pl line 24.
      Should have been encode_entities, and it's available from HTML::Entities. See my updated code.
        Still a problem
        Bareword found where operator expected at test_highlight.pl line 21, n +ear ""<b>" . encode_entities("$2") . "</b" (Might be a runaway multi-line // string starting on line 13) (Missing operator before b?) Scalar found where operator expected at test_highlight.pl line 22, nea +r ": encode_entities("$3" (Might be a runaway multi-line "" string starting on line 21) (Missing operator before $3?) String found where operator expected at test_highlight.pl line 28, nea +r "print text_to_html("" (Might be a runaway multi-line "" string starting on line 22) (Missing semicolon on previous line?) Bareword found where operator expected at test_highlight.pl line 28, n +ear "print text_to_html("best" (Do you need to predeclare print?) syntax error at test_highlight.pl line 21, near ""<b>" . encode_entiti +es("$2") . "</b" Can't find string terminator '"' anywhere before EOF at test_highlight +.pl line 28.
        Got it, should be
        "<\/b>"