Results 1 to 5 of 5
  1. #1

    Highlight words (PHP)

    hi,

    I have a few ideas but i need something a lot better so please help.

    I am trying to highlight all the words that match a given string.

    for example.

    i want something like this,
    http://domain.com/highlight.php?url=...tring=blahblah

    then it should search the yahoo.com's page and highlight all the "blahblah" words.

    Any idea how should i do that or has it been coded before ?

    thanks

  2. #2
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,082
    A clumsy and naive way of doing it would be:
    PHP Code:
    $new preg_replace("/\b($word)\b/i""<span class='highlight'>\1</span>"$string); 
    This is probably a bad idea on any sort of marked-up text, as it'll clobber parameter names and such as well. You need a state machine to fix that.
    Last edited by error404; 12-14-2005 at 01:22 AM.

  3. #3
    thanks for the reply,

    that's what i have in mind but how do you grab words from a given url and "highlight" them ?

  4. #4
    Join Date
    Oct 2005
    Location
    UK
    Posts
    552
    Hello,

    If you wanted to get the words from the url, such as 'highlight.php?highlight=word', you might try doing:

    PHP Code:
    $word $_GET[highlight]; 
    Put that before the preg_replace(); that error404 wrote.
    You might want to do some checks on that as-well.

    I hope that helps , if that's not you wanted, I apoligise,
    - Dark Light.

  5. #5
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,082
    Note that the \1 in my code should be a double backslash (or $1 instead if you're using a non-archaic PHP version).

  6. Newsletters

    Subscribe Now & Get The WHT Quick Start Guide!

Related Posts from theWHIR.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •