LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-29-2004, 09:13 AM   #1
rmanocha
Member
 
Registered: Oct 2003
Location: Austin,TX
Distribution: Debian SID-->fully content-->Love APT,kernel 2.6.4
Posts: 327

Rep: Reputation: 30
PHP script/function to get 4-5 words around a keyword in a string(like google)


hey guys,
I am writing a search engine for a website.
I was just wondering how i would write effecient/nice/clean PHP code for taking 4-5 words around a matched keyword in a string which contains the contents of a text file and then display the results in a similar fashion as google.
I was thinking that this can be done with regexps but since i am no pro with them...i shout out to you guys to help me.
Please suggest something.
Thanks
 
Old 06-29-2004, 01:50 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
Something like this?
Code:
<?

$string = "I was just wondering how i would write effecient/nice/clean PHP code for taking 4-5 words around a matched keyword in a string which contains the contents of a text file and then display the results in a similar fashion as google.";

$word = "just";

preg_match("/(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?$word ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)?/i",$string,$result);

print preg_replace("/$word/","<B>$word</B>",$result[0]);

?>
Will output:
I was just wondering how i would write effecient
 
Old 06-29-2004, 02:47 PM   #3
Hero Doug
Member
 
Registered: Jan 2004
Posts: 41

Rep: Reputation: 15
This will work as well.

PHP Code:
<?php

/* NOTE - It's your responsibility to format the search phrase removing any stupid characters like ^ and # */
/* The search phrase */
$SearchPhrase "linux tutorials articles php here things run test";

/* An example of keywords you might be searching */
$Keywords "This is an example of what you might find in a column containing keywords that are used to search for such things as linux tutorials or help with php. You may also notice that I'm running out of things to say, so I'll end this here.";

/* Find the amount of words */
$Words substr_count($SearchPhrase" ");

/* Explode the search phrase */
$Exploded explode(" "$SearchPhrase);

/* Put the keywords in a variable for highlighting */
$Highlighted $Keywords;

/* Loop through the words */
for($i 0$i <= $Words$i++){

    
/* Highlight the words by overwriting the same set of keywords */
    
$Highlighted preg_replace("/" $Exploded["$i"] . "/""<B>" $Exploded["$i"] . "</B>"$Highlighted);

}

echo 
$Highlighted;

?>

Last edited by Hero Doug; 06-29-2004 at 05:53 PM.
 
Old 06-29-2004, 03:33 PM   #4
rmanocha
Member
 
Registered: Oct 2003
Location: Austin,TX
Distribution: Debian SID-->fully content-->Love APT,kernel 2.6.4
Posts: 327

Original Poster
Rep: Reputation: 30
Thank guys,
I will try both these peices of code firt thing in the mornign and let you know how it goes.
I also wrote some code...but it is really ineffecient...i basically break up the string into an array at every space and then check each string with each search word(nested foreach loop....very ineffecient) and then print out the text around the amtched word.
well newayz...now that u guys have given me this code...i can remove the one i have written and use either of these.
thanks again.
 
Old 06-30-2004, 05:14 AM   #5
rmanocha
Member
 
Registered: Oct 2003
Location: Austin,TX
Distribution: Debian SID-->fully content-->Love APT,kernel 2.6.4
Posts: 327

Original Poster
Rep: Reputation: 30
allright....i tested out the both the codes and the segment given by david_ross seems to work better for my needs.
However i was looking for a few enhancements in the code.As of now..the code only finds the first match in the tet, highlights it and prints it out.
However i want to find all the matches and print them.
This is kind of the thing google does too.It tends to find all the matches and then prints them out.
Also if the search text provided by the user is more than one word, anothe rporblem arises.I changed the code given by david_ross to this:
Code:
$search_arr = explode(" ",$search);
         $out = "....";
         foreach($search_arr as $tmp) {
                             preg_match("/(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?$tmp? ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)?/i",$text,$result);
                             $out .= preg_replace("/$tmp/i","<B>$tmp</B>",$result[0]);
                             $out .= "....";
         }
         return $out;
This works fine as in it finds the correct places and puts the dots in just fine.However the problem arises when the first term of the search word comes after the second word of the search phrase in the text.The output i get is in reverse order...as in i get the text snippet corresponding to the latter text match before relative to the other text match which sould come earlier since it comes first in the text even though the keywod corresponding to it comes second in the seach term.
I hope i have been able to explain my problem well and that someone will be able to help me.
Thanks again.
 
Old 07-12-2004, 07:54 AM   #6
rmanocha
Member
 
Registered: Oct 2003
Location: Austin,TX
Distribution: Debian SID-->fully content-->Love APT,kernel 2.6.4
Posts: 327

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by david_ross
Something like this?
Code:
<?

$string = "I was just wondering how i would write effecient/nice/clean PHP code for taking 4-5 words around a matched keyword in a string which contains the contents of a text file and then display the results in a similar fashion as google.";

$word = "just";

preg_match("/(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?$word ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)?/i",$string,$result);

print preg_replace("/$word/","<B>$word</B>",$result[0]);

?>
Will output:
I was just wondering how i would write effecient
I was just wondering what I can do to make this code faster? currently if i do not include this code into my script...i get a load time of .003 seconds and if this is included...i get a time of 6.34 seconds.granted that I am currently using a P3 500 MHz with 128 MB ram, and that this would be much faster on other machines, I was still wondering if there is any way i can make this faster since there is a lot of difference in load times.
thanks
 
Old 07-12-2004, 09:16 AM   #7
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,578

Rep: Reputation: 127Reputation: 127
To find all matches, just use the g switch in the regex of david_ross code like :
PHP Code:
preg_match("/(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?$word ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)?/ig",$string,$result); 
This way you don't need to loop as the entire text will be parsed and all matches will be printed.
 
Old 07-12-2004, 10:40 AM   #8
rmanocha
Member
 
Registered: Oct 2003
Location: Austin,TX
Distribution: Debian SID-->fully content-->Love APT,kernel 2.6.4
Posts: 327

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by keefaz
To find all matches, just use the g switch in the regex of david_ross code like :
PHP Code:
preg_match("/(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?$word ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)? ?(\w+)?/ig",$string,$result); 
This way you don't need to loop as the entire text will be parsed and all matches will be printed.
thanks, but I now i dont want all matches to be highlighted...only the first match along with the neighbouring few words should be printed out with the search term highlighted.
I have this working right now...but the problem lies in the fact that this is taking too much time. It takes almost 8 seconds to display such a result with 2 hits only.
I need to improve the time and even though a faster machine will change this significantly, I know i can definitely improve on my code too. so again...I call out for any help that is ready to come my way...
sil vous plait.
 
Old 07-13-2004, 06:19 AM   #9
rmanocha
Member
 
Registered: Oct 2003
Location: Austin,TX
Distribution: Debian SID-->fully content-->Love APT,kernel 2.6.4
Posts: 327

Original Poster
Rep: Reputation: 30
anyone...someone....if you are a reg exp guru...please help me out here.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can a function return a string? hubabuba Programming 13 03-06-2005 02:51 PM
passing php string to javascript function djgerbavore Programming 2 03-01-2005 11:34 AM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM
optimize PHP string function Cedrik Programming 6 08-29-2004 09:07 AM
Google Ad-Words advertises MS's "Get the fact campaign" on LQ? Squall General 6 03-23-2004 10:47 PM


All times are GMT -5. The time now is 07:24 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration