SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    eregi_replace - not catching everything

    hey all,

    i have zero knowledge about regular expressions and such, and when i saw the code on the eregi_replace page on php.net i thought i could get off easy

    PHP Code:
    $pattern '(>[^<]*)(' quotemeta($query) . ')';
    $replacement '\\1<span class="termhighlight">\\2</span>';
    $body eregi_replace($pattern$replacement$body); 
    thats what im using to highlight a searchterm in an document requested by the user. the problem is it's not highlighting all occurances of the word on the page.

    here's the site im testing it on: http://www.thedowneypatriot.net/view...1&query=rancho

    (thats the only article in the database atm, im just testing things out)


    is there anything obvious i've missed?

    thanks for your time

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    $pattern = '~\b(' . $query . ')\b~i';
    $replacement = '<span class="termhighlight">\\1</span>';
    $body = preg_replace($pattern, $replacement, $body);
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i replaced my code with yours, nothing gets highlighted now

    edit: nm i forgot to change it to preg_replace. this worked excellently, ty for your time!!


Bookmarks

Posting Permissions

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