Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
  1. #1
    Senior Member
    Join Date
    Oct 2007
    Posts
    241
    Member #
    15910
    Lets say you have a mysql search query that searches a keyword in a table that contains long text, such as an article text...I have no problem bringing up the row from the table, which contains that keyword...
    However, how do I display only a certain number of characters before and after that keyword? I can not display the whole article body for each result as that is too big...Also how can I highlight the keyword in the displayed result?

  2.  

  3. #2
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    1. Get the strpos of the keyword.
    2. Then get a substring from the result which is say 50 characters before the keyword and 50 after.
    3: You could replace the keyword with <p id="keyword">[KEYWORD_HERE]</p> and have the CSS id doing the highlighting.

  4. #3
    Senior Member
    Join Date
    Oct 2007
    Posts
    241
    Member #
    15910
    This really looks like a good solution. However I am not sure how exactly I will do steps 1 and 2. Are you able to provide more detail if possible? Thanks a lot.

  5. #4
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    I am at work just now, I quickly threw this together. It does what you want...

    HTML Code:
    <html>
    
    <head>
    
    <title>PHP highlight word and display text before/after keyword</title>
    
    <style type="text/css">
    
    span#highlight
       {
    	color: red;
    	display: inline;
       }
    
    p
       {
    	margin-bottom: 20px; 
       }
    
    </style>
    
    </head>
    
    <body>
    
    
    <?php
    
    $string = "This is my test string ketanco, I am not sure of the keyword we will use. What do you think? I know. The keyword is banana.";
    $string .= "Why did I choose that? Well it is my favourite fruit. I am just typing here so there is more than 50 characters after the keyword!";
    
    // get position of keyword
    $keyword = "banana";
    $pos = strpos($string, $keyword); // returns position in int of our keyword
    
    if($pos === false)
       {
    	echo "Our keyword was not found.";
       }
    else
       {
    	echo "The keyword $keyword was found at position $pos.";
       }
    
    // now get substring after some spacing
    
    echo "<br /><br />\n\n";
    
    // take away 50 from our pos var
    $pos = $pos - 50;
    $end = $pos + 50;
    
    $output = substr($string, $pos, $end);
    
    // now replace the keyword with <p id="highlight">KEYWORD</p>
    
    $replace = str_replace($keyword, "<span id=\"highlight\">$keyword</span>", $output);
    
    echo "<p>Original String = $string</p>\n\n";
    
    echo "<p>String Before Replace = $output</p>\n\n";
    
    echo "<p>String After Replace = $replace</p><br />";
    
    ?>
    
    </body>
    </html>
    Note: The only major drawback is that because we just minus and add 50 to get 50 characters before and after banana we may output some incomplete words, you will see what I mean.

    http://andrewbuchan.co.uk/testing/highlight.php

    Cheers

  6. #5
    Senior Member Eddy Bones's Avatar
    Join Date
    Jan 2004
    Location
    Washington, USA
    Posts
    1,054
    Member #
    4651
    Quote Originally Posted by bfsog
    Note: The only major drawback is that because we just minus and add 50 to get 50 characters before and after banana we may output some incomplete words, you will see what I mean.
    Good point, and easily fixed. If the last character in the string is not a space, subtract a character, then check again. Obviously this will be in a loop. Once you hit a blank space you know you're at the end of a full word.

    This concept can be applied to the beginning of the string too, of course.

  7. #6
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Yeah, I was going to post a solution with that in mind at some point.

    Another way would be to explode the sentence with the space " " delimeter and then find which array position contains the keyword and then show x array values before and x array values after, remembering to put a space " " in between them or yoursentencewilllooklikethis.

  8. #7
    Senior Member
    Join Date
    Oct 2007
    Posts
    241
    Member #
    15910
    ok..I cant get it to work for some reason...here is my code..
    first i get the keyword from the search button on another page...
    then that keyword is searched in the table.. which is id/title/date/par1/body by the way
    the rows including the keyword is returned with no problem and i can display them as i want.
    however i can not do the trick of displaying -+50 characters before and after keyword...also it does not give accurate positions of the words I search. what's wrong below? so i didnt even try of highlighting yet, first i gotta resolve this, which is seeing the keyword with 50 characters before and after it....
    $keyword1=$_POST['keyword1']; //i get keyword from a search form
    $trimmed1 = trim($keyword1);
    $query=mysql_query("SELECT * FROM newsa WHERE title LIKE '%$trimmed1%' OR par1 LIKE '%$trimmed1%' OR body LIKE '%$trimmed1%' ORDER BY date DESC LIMIT 100");
    while ($row = mysql_fetch_array($query)) {
    echo"$row['date']."<br/>". $row['title'] . "</p>". $row['par1']."<br/>"."<br/>"; //this displays correctly the rows which includes the search keyword...so up to here it was fine but it doesnt necessarily show my keyword....below here, i am also trying to display -+50 characters before and after keyword so that user can see it. i was going to remove here if the code below was working...so here is what i did:
    $string=$row['title'].$row['par1'].$row['body']; //in order to search the keyword in title + first paragraph + the rest of the article

    $pos = strpos($string, $trimmed1); //i can not get the corrct position...plus if there is more than one keyword in string, which one is supposed to be returned?

    echo "$pos"."<br/>"; //this is just to see the position number for my info
    $pos = $pos - 50;
    $end = $pos + 50; //actually this is of no use am I wrong? because in substr the last item is the length of string to be displayed, not the end point...so i wrote 20 for testing...
    $output = substr($string, $pos, 20);
    echo "$output";.

  9. #8
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    What I think you might be doing wrong is setting $string to $row['title'].$row['par1'].$row['body'];

    That means that $trimmed has to contain the EXACT same string value. The more variables you concatenate into $string the lesser the chances of strpos giving you a correct value.

  10. #9
    Senior Member
    Join Date
    Oct 2007
    Posts
    241
    Member #
    15910
    Then do you know how can i locate the keyword in the whole text, which is title+par1+body?
    Also I didn't understand your second sentence exactly, and why is it so...
    Thanks for helping

  11. #10
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Okay.
    Take this as an example.

    Your keyword is Countires.
    Your par1 is I love big countries.
    Your body is Spain is quite big.

    That means that the search term you need to find is

    CountriesI love big countriesSpain is quite big

    You see, when you were building $string you were not giving it any spaces in between.

    So lets say I enter the below in your search box

    Countries I love big countries Spain is quite big

    Then I call strpos

    PHP Code:
    // $string is the keyword + " " + $par1 + " " + $body
    // $searchTerm is whatever you enter into search box
    // $pos is the position of $searchTerm in $string
    $position strpos($string$searchTerm); 
    Now because presumably you have your keyword at the beginning of the entry, there are no words before it to get.

    I think you need to clarify what you want to happen and we will take it from there.


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:23 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com