SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Google-like highlighting function displays .html tags

    I just implemented a function called str_highlight to highlight a string in text without corrupting HTML tags. The source is:
    http://aidan.dotgeek.org/lib/?file=f..._highlight.php

    I implemented the function using this code:
    PHP Code:
    $s addslashes($queryString[2]);
    $s explode(' ',$s);
    .....
    $string $rowPresenter["biography"];
    $search $s;
    echo 
    str_highlight($string$searchSTR_HIGHLIGHT_SIMPLE|STR_HIGHLIGHT_WHOLEWD); 
    It works great on the following page using a search term in the url.
    http://test.kripalu.org/presenter/498/roth
    That is, it highlights "roth" in the text, but not in the url, which would corrupt the link.

    However, when I go to the page without a search term, some of the .html tags show.
    http://test.kripalu.org/presenter/498/
    Any clues why?
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  2. #2
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    EDIT: just found something with the script. will test and post back
    ---------------------------
    Errors = Improved Programming.
    My Site

  3. #3
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Regina, SK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is your html. Its not valid.
    Viewing the source, I see
    HTML Code:
    <<strong></strong>p<strong></strong>><strong></strong>David<strong>
    That's really ugly. This amounts to the same thing (though valid html)

    HTML Code:
    <p>David
    It probably has something to do with

    PHP Code:
    $s explode(' ',$s);
    .....
    $search $s
    Depending on what's in between there, I'm not sure exactly what you're passing to that function when no search term is specified. I would suggest doing something like:

    PHP Code:
    if (empty($s)) {
      echo 
    $rowPresenter["biography"];
    } else {
      
    $s addslashes($queryString[2]);
      
    $s explode(' ',$s);
      .....
      
    $string $rowPresenter["biography"];
      
    $search $s;
      echo 
    str_highlight($string$search,     STR_HIGHLIGHT_SIMPLE|STR_HIGHLIGHT_WHOLEWD); 

    Reduce or eliminate spam on your sites without CAPTHCAs
    - maybe it is possible: http://formantispam.thekerrs.ca/

  4. #4
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its actually not his code, its the script (As i tested it)
    when he uses STR_HIGHLIGHT_SIMPLE|STR_HIGHLIGHT_WHOLEWD and leave the search string empty, it shows the HTML tags, i don't know why, it means script needs to be fixed.
    ---------------------------
    Errors = Improved Programming.
    My Site

  5. #5
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, guys. That code did really get ugly! Here's how it looks in the database.

    HTML Code:
     <p>David Roth is a singer-songwriter whose resume includes top honors at two of the country’s premier songwriter festival competitions, .... <a href="http://www.davidrothmusic.com" target="_blank">www.davidrothmusic.com</a></p>
    I'll try the "else" statement.
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  6. #6
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Regina, SK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The else should work, and actually be faster than not using it. However, looking at Aiden's code, I can see why its happening. Basically, if you send it an empty string, its going to replace the empty string with the default highlight syntax, which happens to be strong.
    Reduce or eliminate spam on your sites without CAPTHCAs
    - maybe it is possible: http://formantispam.thekerrs.ca/

  7. #7
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nos
    The else should work, and actually be faster than not using it. However, looking at Aiden's code, I can see why its happening. Basically, if you send it an empty string, its going to replace the empty string with the default highlight syntax, which happens to be strong.
    i think that might be it. so if no search term then don't call the function. or you can change function to do nothing if search term is empty.
    ---------------------------
    Errors = Improved Programming.
    My Site

  8. #8
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I removed STR_HIGHLIGHT_SIMPLE| and that took care of the .html tags, however the script still prints special characters literally.

    So, I tried the "if..else" solution. The script still prints special characters literally.
    http://test.kripalu.org/presenter/498/

    If I go back to my original script,
    PHP Code:
    echo($rowPresenter["biography"]); 
    special characters are displayed correctly.

    I can't see what is different between the original script and the current version of the "if" clause.

    PHP Code:
    if (empty($s)) { 
    echo 
    $rowPresenter["biography"]; 
    } else { 
    $string $rowPresenter["biography"]; 
    $search $s
    echo 
    str_highlight($string$searchSTR_HIGHLIGHT_WHOLEWD); 

    except that "biography" passes thru

    PHP Code:
    if (empty($s)) { 
    echo 
    $rowPresenter["biography"]; 
    Why would that cause special characters to be displayed literally?
    Last edited by paulcj2; Jun 1, 2005 at 07:13.
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  9. #9
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As the problem has evolved, I am starting a new thread which focuses more clearly on the problem as I understand it now.
    Paul C.
    ClickBasics
    http://www.clickbasics.com


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
  •