FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


search, a bit contextual in php





bukaida
I have the following scenario. I have a piece of text string, on which I want to search for a keyword.
If the word is found in the string, I want to fetch the immediate four words after the keyword( if four words are not available, then the rest of the words)in the string.So the result returned should be a substring starting with the keyword followed by the next four words.All kinds of punctuation marks should be ignored. I guess some kind of trimming operation is required but donot know how to, in PHP. Please Help.
jmraker
I'm assuming you're using something like stripos and substr beforehand to get the words after the keyword into a string.

You can either split up the rest of the words into an array using explode(' ', $string, 4+1), or loop through the rest of the words one letter at a time looking for spaces and stop when you find 4 or the end of the string.

http://us.php.net/stripos
http://us.php.net/substr
http://us.php.net/explode
sonam
Something like this:
Code:
<?php
$word = 'kinds';
$punct = array(',', '.', "'", '?', '!', '"', '#', '$', '%', '&', '/', '@'); //put more here
$string = 'I have the following scenario. I have a piece of text string, on which I want to search for a keyword.
If the word is found in the string, I want to fetch the immediate four words after the keyword( if four words are not available, then the rest of the words)in the string.So the result returned should be a substring starting with the keyword followed by the next four words.All kinds of punctuation marks should be ignored. I guess some kind of trimming operation is required but donot know how to, in PHP. Please Help.';
str_replace($punct, ' ', $string);

if(strpos($string, $word) !== FALSE) {
   $new_string = explode($word, $string);
   $four = str_word_count($new_string[1], 1);
}
echo $four[0] . "<br />";
echo $four[1] . "<br />";
echo $four[2] . "<br />";
echo $four[3] . "<br />";
?>
bukaida
Thanx Sonam, Giving your code a try. Let you know the result.
BTW, I am working with UTF-8 characters. So can I put the unicode values of certain puctuation marks (like 'া') inside the punctuation array of your code?
bukaida
@Sonam: The code is working only for the first occurence of the key word. If the keyword appears more than once, ( like 'is' in your example), then it returns only the result for the first 'is'. Any workaround?
sonam
I think you can put unicode in array. Did you want randomly to display results or it must go one by one, mean first, second, third...

Sonam
bukaida
If the result can be displayed in order as they are found, it will be the best solution. Otherwise random display will also be ok. But all results must come.

However the unicode is creating a bit problem (I am getting boxes). Actually the string is coming from mysql's select query and displaying properly with an echo $variable. After running your code, it is picking 4 things but they are displaying as box. I am giving the code below--

Code:

$punct = array(',', '.', "'", '?', '!', '"', '#', '<', '%', '&', '/', '@','>','b','u','font'); //put more here
foreach ( $res2 as $entry )
      {
                                      $count=substr_count($entry,$key);
      
      $string=$entry;
      $word=$key;
      if(strpos($string, $word) !== FALSE) {
   $new_string = explode($word, $string);
   $four = str_word_count($new_string[1], 1);
}       
          echo $count;
          echo'<br>';
          echo $four[0] . "<br />";
echo $four[1] . "<br />";
echo $four[2] . "<br />";
echo $four[3] . "<br />";
          echo'<br>';
         //print "$entry\n";
         
      }

Thanx and Regards
Fire Boar
A few things.

- What did you do to the indentation?! It went from beautiful to unreadable.
- You've removed the punctuation strip code, which is quite important.
- You can't put text in the punctuation array, it will only end up deleting every instance of that text. If you want to strip HTML tags, make a regular expression and run it before stripping punctuation.

My approach would be a linear search like so.

Code:
<?php
function keyword_search_four($haystack, $needle) {
  $needle = mb_strtolower($needle);
  $words  = explode(' ', $haystack);
  $count  = count($words);
  $result = array();
  for ($i = 0; $i < $count; $i++) {
    if (mb_strpos(mb_strtolower($words[$i]), $needle) !== false) {
      $result[$i] = $words[$i];
      if ($count > $i+1): $result[$i] .= ' ' . $words[$i+1];
      if ($count > $i+2): $result[$i] .= ' ' . $words[$i+2];
      if ($count > $i+3): $result[$i] .= ' ' . $words[$i+3];
      if ($count > $i+4): $result[$i] .= ' ' . $words[$i+4];
      endif; endif; endif; endif;
    }
  }
  return $result;
}


If you need to match a word exactly, or need to deal with poor grammar (specifically words separated only by a punctuation mark and no space), it will need some tweaking and a punctuation whitelist or blacklist needs to be added.
sonam
I don't know about unicode but I don't think how script and displaying results are in correlation. If you give right doctype and charset it must display right characters. BTW if you want to remove html tags then you need some other php function strip_tags.
Code:
<?php
$word = 'words';
$punct = array(',', '.', "'", '?', '!', '"', '#', '$', '%', '&', '/', '@'); //put more here
$string = 'I have the following scenario. I have a piece of text string, on which I want to search for a keyword.
If the word is found in the string, I want to fetch the immediate four words after the keyword( if four words are not available, then the rest of the words)in the string.So the result returned should be a substring starting with the keyword followed by the next four words.All kinds of punctuation marks should be ignored. I guess some kind of trimming operation is required but donot know how to, in PHP. Please Help.';
str_replace($punct, ' ', $string);

if(strpos($string, $word) !== FALSE) {
   $new_string = explode($word, $string);
   $count = count($new_string);
}

for ($i = 1; $i <= $count-1; $i++) {   
    $four[] = str_word_count($new_string[$i], 1);
}

foreach($four as $key => $val){
$num = $key + 1;
echo '<h1>' . $num . '.</h1>';
echo $val[0] . "<br />";
echo $val[1] . "<br />";
echo $val[2] . "<br />";
echo $val[3] . "<br />";
}
?>
[/code]
bukaida
Thanx sonam. Sorry for late response. Trying your code. I will post the result here. BTW the indentation was deliberately made like that in R&D, just to identify the newly inserted code easily(Although not a very good approach Very Happy ).
bukaida
@sonam

Your code, 2nd one is working for english characters but not for UTF-8. I have attached the output of a search.



The code I have used
Code:

<?php header("Content-Type: text/html; charset=UTF-8"); ?>//to display UTF characters
-----some more codes here--
$punct = array(',', '.', "'", '?', '!', '"', '#', '$', '%', '&', '/', '@'); //put more here
$word = $key;
--Your code--
foreach ( $res2 as $entry )
      {
         
         $pattern1='-(-';
         $replacement1='<br><B>-(-';
         $pattern2=')-';
         $replacement2=')-</B><br>';
         
         $entry = str_replace($pattern1,$replacement1,$entry);
         $entry = str_replace($pattern2,$replacement2,$entry);
         
         echo('<b>Total Count of<u> ');echo $key;echo('</u> in <u>');echo $title; echo(':</u></b> ');
          $count=substr_count($entry,$key);
            
          echo $count;
          echo'<br>';
          $string=strip_tags($entry);
          str_replace($punct, ' ', $string);

if(strpos($string, $word) !== FALSE) {
   $new_string = explode($word, $string);
   $count = count($new_string);
}

for ($i = 1; $i <= $count-1; $i++) {   
    $four[] = str_word_count($new_string[$i], 1);
                           }

foreach($four as $key => $val){
$num = $key + 1;
echo '<h2>' . $num . '.</h2>';
echo $word;echo('&nbsp;');
echo $val[0]; echo('&nbsp;');
echo $val[1];echo('&nbsp;');
echo $val[2]; echo('&nbsp;');
echo $val[3] . "<br />";
                              }
                             
print "$entry\n";
         
   }
      echo'<br>';
      
   }


What is the solution?
sonam
Did you have this page online, if you have please send me/post link here. If you haven't then create full html page. Cut my script in two peaces. Second part start with foreach function and you need to put it in body of your html page.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
foreach($four as $key => $val){
$num = $key + 1;
echo '<h1>' . $num . '.</h1>';
echo $val[0] . "<br />";
echo $val[1] . "<br />";
echo $val[2] . "<br />";
echo $val[3] . "<br />";
}
?>
</body>
</html>
bukaida
Sorry, It is not online yet. However the html code for the output page is

Code:

<html>
<head>
<title> Tagore Archive</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
 
<script type="text/javascript">
 
 
function zoomin(){
   
 
if(parent.parent.document.body.style.zoom!=0)
parent.parent.document.body.style.zoom*=1.4;
else
parent.parent.document.body.style.zoom=1.4;
 
}
</script>
</head>
 
 
<body background="back.GIF" onload="zoomin()">
<table width="100%" align="center">
<tr>
<td colspan="360" align="center"><p><h2><b><font face="Century Gothic" size="+3" color="grey">রবীন্দ্র রচনাবলী</font></b></h2></p>
</td>
</tr>
<tr>
<td align="center" bgcolor="#0099ff"><b><a href="index.php" title="Home"><h2>মূল পৃষ্ঠা</h2></b></a></td>
<td align="center" bgcolor="#0099ff"><h2><a href="insert_form.php" title="Insert Record">তথ্য সংযোগ</h2></a></td>
<td align="center" bgcolor="#0099ff"><h2><a href="index.php" title="Search">তথ্য সন্ধান</a></h2></td>
<td align="center" bgcolor="#0099ff"><h2><a href="delete.php" title="Delete Record">তথ্য বিলোপ</h2></a></td>
<td align="center" bgcolor="#0099ff"><h2><a href="help.php" title="Help" >সাহায্য</h2></a></td>
</tr>
</table>
</body>
</html>
 
<br><br><br><h1 align="center"><u>শেষের কবিতা</h1></u><b>Total Count of<u> earth</u> in <u>শেষের কবিতা:</u></b> 1<br>earth    <br><br>....  কাছে যাওয়া মুলতবি রেখেছে । বরঞ্চ লেখা আছে সাঁতার দিয়ে অগাধ জল পার হওয়ার কথা । কিন্তু সেটা অন্তরের ইতিহাস, সেখানকার সমুদ্রে আমিও কি সাঁতার কাটছি নে ভাবছ । সে অকূল কোনোকালে কি পার হব ।
For we are bound where mariner has not yet dared to go,
And we will risk the ship, ourselves and all. 
আমরা যাব যেখানে কোনো
যায় নি নেয়ে সাহস করি,
ডুবি যদি তো ডুবি-না কেন
ডুবুক সবই, ডুবুক তরী ।
বন্যা, আমার জন্যে আজ তুমি অপেক্ষা করে ছিলে ?
হাঁ মিতা, বৃষ্টির শব্দে সমস্ত দিন যেন তোমার পায়ের শব্দ শুনেছি । মনে হয়েছে, কত অসম্ভব দূর থেকে যে আসছ তার ঠিক নেই । শেষকালে তো এসে পৌঁছলে আমার জীবনে ।
বন্যা, আমার জীবনের মাঝখানটিতে ছিল এতকাল তোমাকে-না-জানার একটা প্রকাণ্ড কালো গর্ত । ঐখানটা ছিল সব চেয়ে কুশ্রী । আজ সেটা কানা ছাপিয়ে ভরে উঠল তারই উপরে আলো ঝল্‌মল্‌ করে, সমস্ত আকাশের ছায়া পড়ে, আজ সেইখানটাই হয়েছে সব চেয়ে সুন্দর । এই-যে আমি ক্রমাগতই কথা কয়ে যাচ্ছি, এ হচ্ছে ঐ পরিপূর্ণ প্রাণসরোবরের তরঙ্গধ্বনি ; একে থামায় কে ।
মিতা, তুমি আজ সমস্ত দিন কী করছিলে ।
মনের মাঝখানটাতে তুমি ছিলে, একেবারে নিস্তব্ধ । তোমাকে কিছু বলতে যাচ্ছিলুম কোথায় সেই কথা । আকাশ থেকে বৃষ্টি পড়ছে আর আমি কেবলই বলেছি, কথা দাও, কথা দাও !
O, what is this ?
Mysterious and uncapturable bliss <br><B>-(-পৃ- ৪৯৬ )-</B><br>
That I have known, yet seems to be
Simple as breath and easy as a smile,
And older than the <b><u><font color='green'>earth</font></u></b>.
একি রহস্য, একি আনন্দরাশি !
জেনেছি তাহারে, পাই নি তবুও পেয়ে ।
তবু সে সহজে প্রাণে উঠে নিশ্বাসি,
তবু সে সরল যেন রে সরল হাসি,
পুরানো সে যেন এই ধরণীর চেয়ে ।
বসে বসে ঐ করি । পরের কথাকে নিজের কথা করে তুলি । সুর দিতে পারতুম যদি তবে সুর লাগিয়ে বিদ্যাপতির বর্ষার গানটাকে সম্পূর্ণ আত্মসাত্‍ করতুম
বিদ্যাপতি কহে, কৈসে গোঙায়বি
হরি বিনে দিন রাতিয়া ।
যাকে না হলে চলে না তাকে না পেয়ে কী করে দিনের পর দিন কাটবে, ঠিক এই কথাটার সুর পাই কোথায় । উপরে চেয়ে কখনো বলি কথা দাও, কখনো বলি সুর দাও । কথা নিয়ে সুর নিয়ে দেবতা নেমেও আসেন, কিন্তু পথের মধ্যে মানুষ ভুল করেন, খামকা আর কাউকে দিয়ে বসেন হয়তো-বা তোমাদের ঐ রবি ঠাকুরকে ।
লাবণ্য হেসে বললে, রবি ঠাকুরকে যারা ভালোবাসে তারাও তোমার মতো এত বার বার করে তাঁকে স্মরণ করে না ।
বন্যা, আজ আমি বড়ো বেশি বকছি, না ? আমার মধ্যে বকুনির মন্‌সুন নেমেছে । ওয়েদার-রিপোর্ট যদি রাখ তো দেখবে, এক-এক দিনে কত ইঞ্চি পাগলামি তার ঠিকানা নেই । কলকাতায় যদি থাকতুম তোমাকে নিয়ে টায়ার ফাটাতে ফাটাতে মোটরে করে একেবারে মোরাদাবাদে দিতুম দৌড় । যদি জিজ্ঞাসা করতে মোরাদাবাদে কেন তার কোনোই কারণ দেখাতে পারতুম না । বান যখন আসে তখন সে বকে, ছোটে, সময়টাকে হাসতে হাসতে ফেনার মতো ভাসিয়ে নিয়ে যায় ।
এমন সময় ডালিতে ভরে যোগমায়া সূর্যমুখী ফুল আনলেন । বললেন, মা লাবণ্য, এই ফুল দিয়ে আজ তুমি ওকে প্রণাম করো ।
এটা আর কিছু নয়, একটা অনুষ্ঠানের মধ্যে দিয়ে প্রাণের ভিতরকার <br><br>....
<br>

Is that what you wanted?
sonam
OK then you MUST put on the very top of page...

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">


...instead of blank <html> because DOCTYPE is very important. Then put second part of my script (explained in post before) on the end of your page but before body and html tag. And see what is output.

BTW if I am seeing good your (hindi) text is out of body and html tag. In that case isn't possible to get unicode result.

Sonam
bukaida
@Sonam
Thank you very much for your quick response. Actually I am using sphinx engine for the searching operation and the output shown in html tag is for the top menu include page (include menu.php)only.The dynamic part is outside the html. The sphinx search engine is giving the result as a string and I am trying to post-process the result. should I put the html tags under php echo? Moreover the string returned by sphinx is in unicode and displayed properly but the post-processed result is not.

BTW the script is bengali and the output is from the part of a story"Sesher Kobita" by famous bengali writer Rabindranath Tagore. You have to Zoom your browser to 150% to see it properly.Very Happy
sonam
Quote:
BTW the script is bengali and the output is from the part of a story"Sesher Kobita" by famous bengali writer Rabindranath Tagore.


Yeah, I recognize Indian letters but I don't know which one it is. For me bengali and hindu is very similar. Only what I can sometimes to recognize is Telugu because is more rounded. Rabindranath Tagore is famous writer from India.

Sonam
bukaida
Thanx Sonam. But keeping aside the literature thing, any help regarding the code? I am realy stuck with it. It seems from your previous post that little can be done in this scenario (as it is outside the body tag). I hope I am wrong.
Fire Boar
Use the mb_ functions instead of the str_ functions. They are functionally equivalent, except str_ does not work for characters over one byte in length.
bukaida
Thanx fireboar. I am trying your suggestion.
sonam
I really don't know. I have program "Crismon Editor" what have options Document -> Encoding Type -> "UTF8 Encoding (w/o boom)". I think Notepad++ have similar options. In that case your output is encoded before outputing.

Sonam
bukaida
Thanx sonam. Is there any UTF type casting available?
bukaida
I have found the problem but cannot solve it.
Code:

$string=strip_tags($entry);
          str_replace($punct, ' ', $string);

if(strpos($string, $word) !== FALSE) {
   $new_string = explode($word, $string);
   $count = count($new_string);
   echo 'Total words:'; echo $count;echo'<br>';
   
   }

for ($i = 1; $i <= $count-1; $i++) {   
   // $four[] = str_word_count($new_string[$i], 1);

//FUNCTION str_word_count() CAUSED THE UTF PROBLEM. SO I SUBSTITUTED IT WITH
   
     $four[]= count(preg_split($key, $new_string[$i], -1,PREG_SPLIT_NO_EMPTY));
     
                           }

foreach($four as $key => $val){
$num = $key + 1;
//UNFOTUNATELY NO OUTPUT FOR THE CODE BELOW

echo $word;echo(' ');
echo $val[0]; echo(' ');
echo $val[1];echo(' ');
echo $val[2]; echo(' ');
echo $val[3] ;



I tried with the substitution
// $four[] = str_word_count($new_string[$i], 1);

$four[]= count(preg_split($key, $new_string[$i], -1,PREG_SPLIT_NO_EMPTY));
as the UTF problem lies in str_word_count. Please help.
sonam
Maybe you can try this one. I am change script in some other way. Try it Smile

Code:
<?php
$word = 'words';
$punct = array(',', '.', "'", '?', '!', '"', '#', '$', '%', '&', '/', '@'); //put more here
$string = 'I have the following scenario. I have a piece of text string, on which I want to search for a keyword. If the word is found in the string, I want to fetch the immediate four words after the keyword( if four words are not available, then the rest of the words)in the string.So the result returned should be a substring starting with the keyword followed by the next four words. All kinds of punctuation marks should be ignored. I guess some kind of trimming operation is required but donot know how to, in PHP. Please Help.';
$string = str_replace($punct, ' ', $string);

$punct2 = array('  ', '   ', '    ', '     '); // this will clean up to 5 spaces
$string = str_replace($punct2, ' ', $string);

if(strpos($string, $word) !== FALSE) {
   $new_string = explode($word, $string);
   $count = count($new_string);
} else {
   echo "<h3>Sorry $word not exist!</h3>";
   exit;
}

for ($i = 1; $i <= $count-1; $i++) {
    $new_string[$i] = trim($new_string[$i]);
    $val = explode(" ", $new_string[$i]);
   
echo '<h3>' . $i . '.</h3>';
echo $val[0] . "<br />";
echo $val[1] . "<br />";
echo $val[2] . "<br />";
echo $val[3] . "<br />";
}

?>


Sonam
bukaida
Thanx sonam. Giving it a try. I will inform you about the result here.
bukaida
@sonam.

You are simply fantastic. Things are working till now. Just one thing, instead of four different words, I require a string consisting of those word. Do I need to use implode( ) for this purpose or there is a better way ? And to include the keyword with every result and display it in single line, I have to concatinate it with the result like--
Code:

echo '<b>' . $i . '.</b>';
echo '<b>' . $key . ' </b>';
echo $val[0]." ";
echo $val[1]." ";
echo $val[2]." ";
echo $val[3] . "<br />";


Is there any better way out?

And last but not the least
THANX THANX THANX THANX THANX THANX THANX THANX THANX THANX THANX THANX THANX
Nemesis234
bukaida wrote:
@sonam.

You are simply fantastic. Things are working till now. Just one thing, instead of four different words, I require a string consisting of those word. Do I need to use implode( ) for this purpose or there is a better way ? And to include the keyword with every result and display it in single line, I have to concatinate it with the result like--
Code:

echo '<b>' . $i . '.</b>';
echo '<b>' . $key . ' </b>';
echo $val[0]." ";
echo $val[1]." ";
echo $val[2]." ";
echo $val[3] . "<br />";


Is there any better way out?

And last but not the least
THANX THANX THANX THANX THANX THANX THANX THANX THANX THANX THANX THANX THANX


Code:
print"<b> $i $key</b>";
foreach ($val as $value)
{
print"$value";
}
print"<br>";

also for clearing an unknown amount of spaces
$string= trim(preg_replace('/\s\s+/', ' ', $string));
sonam
Like Nemesis234 point out you can this two lines

Code:
$punct2 = array('  ', '   ', '    ', '     '); // this will clean up to 5 spaces
$string = str_replace($punct2, ' ', $string);


to change with this one
Code:
$string= trim(preg_replace('/\s\s+/', ' ', $string));


Thanks Nemesis234. Very Happy


If you want to echo in line just use this. I am put $word instead of $key because I don't use $key anywhere. But if you get this variable from some other side just change it.

Code:
for ($i = 1; $i <= $count-1; $i++) {
    $new_string[$i] = trim($new_string[$i]);
    $val = explode(" ", $new_string[$i]);
   
echo '<strong>' . $i . '. ' . $word . ': </strong>' . $val[0] . " " . $val[1] . " " . $val[2] . " " . $val[3] . "<br />";
}


Sonam

P.S.
I am not use <b> tag because this is not valid CSS Wink
bukaida
Thanx to both of you. But if I need to pass the four values as a string like-

Code:

// something like

$pass_string=$val[0]+$val[1]+$val[2]+$val[3];

//Above sentence is for  conceptual purpose only, not syntacticaly correct.



What should I do?
Nemesis234
php's built in implode function will do just fine for that
sonam
This is little bit tricky question. In example you have four times word "words" (on the end 16 words in result). Now is question did you need four different strings (with different names)...

Code:
$string1 = $val . $val2 . $val3 . $val4
$string2 = $val . $val2...
$string3 = $val . $val2...
$string4 = $val . $val2...


...or one string with all 16 results...

Code:
$string_all = $val . $val2 . $val3 . $val4 . $val . $val2 . $val3 . $val4 ...


...or an array (then you can foreach values and get result separately) but this is not string Very Happy

Code:
$string_for = array(0=>$val . $val2 . $val3 . $val4, 1=>$val . $val2 . $val3 . $val4, ....);


Sonam
bukaida
It is just required to combine each result independently.i.e

1.keyword+3 adjecent word

2.keyword+3 adjecent word

3.keyword+3 adjecent word

and so on.

BTW, a small problem is coming. When I am searching for a word say "range" and there is a word "orange", the program is trying to extract the "range" from "orange" and the resultant word array is looking meaningless. So it will be very nice if this interword stripping can be stopped and entire word can be fetched (i.e exact search instead of patial search).
Many Thanx again in advance.
sonam
Ok, then change last part of script in that one:

Code:
for ($i = 1; $i <= $count-1; $i++) {
    $new_string[$i] = trim($new_string[$i]);
    $val = explode(" ", $new_string[$i]);
    $r=$i-1;   
    $result[$r] = '<strong>' . $i . '. ' . $word . ': </strong>' . $val[0] . " " . $val[1] . " " . $val[2] . " " . $val[3] . "<br />";
}


Now you can echo all results at ones with foreach function...
Code:
foreach($result as $val) {
   echo $val;
}


or one by one (but in that case you must know how many result you are get it.
Code:
echo $result[0]; // display first result
echo $result[1]; // second
echo $result[2]; // etc.


About range/orange you can put space in front and on the end of word. If you need to do this automatically then put this line after $word definition...

Code:
$word = ' ' . $word . ' ';


...but this will exclude for example (range) in brackets Smile

Sonam
bukaida
Thanx Sonam.Trying the modified solution.I will post the result here.
Nemesis234
Code:
<?php
$word = 'of';

$punct = array(',', '.', "'", '?', '!', '"', '#', '$', '%', '&', '/', '@', '(' , ')'); //put more here

$string = 'I have the following scenario. I have a piece of text string, on which I want to of search for of  of a keyword.
If the word is found in the string, I want to fetch the immediate four words after the keyword( if four words are
 not available, then the rest of the words)in the string.So the result returned should be a substring starting with
 the keyword followed by the next four words. All kinds of punctuation marks should be ignored. I guess some kind of
 trimming operation is required but donot know how to, in PHP. Please Help.';

$string = str_replace($punct, ' ', $string);
$string= trim(preg_replace('/\s\s+/', ' ', $string));

if(strpos($string, $word) === FALSE) {
   echo "<h3>Sorry $word does not exist!</h3>";
   exit;
}

$array = explode(" ", $string);

$incount = FALSE;
$x=1;
$y=1;
foreach($array as $val){
if(($val == "$word") OR ($incount === TRUE)){
$incount = TRUE;
$wordcount[$x][$y] = $val;
$y++;
}
if($y > '5'){
$incount = FALSE;
$x++;
$y=1;
}

}
$z = 1;
while($x > '0'){
while($z <= '5'){
print"".$wordcount[$x][$z]." ";
$z++;
}
print"<br>";
$z=1;
$x--;
}

?>

i have found that sonams example will not cope with 2 words which you search for being close.

i.e. if i search "of" in "this is of great importance of the world" sonams will return some arrays with less than the 4 words required.

ive just done a quick write up which works with multi-dimensial arrays, its not brilliant but it will worth on exact words only and with occurances of words close together, by ignoring them if they are within 4 words of the last occurance.

outputs are used as follows
wordcount[first occurance]
[first word]
[second word]
[third word]
[fourth word]
wordcount[second occurance]
[first word]
[second word]
[third word]
[fourth word]
etc..
bukaida
Thanx nemesis234. Posting the result shortly.
bukaida
@nemesis234
So far it is working perfectly. I will run it couple of more times to be sure. And of course, many many thanx to both of you.

EDIT

Just found out that nemesis234's logic is working for single word only, not for multi-word string.i.e

working well for "GOOD" or "BOY", but not for "GOOD BOY". May be the blank space between the word is creating the problem. Help Sad

@sonam
Your code is working with multiwords.However the modified code is not printing anything.I have made a substitution of the code

Code:

for ($i = 1; $i <= $count-1; $i++) {
    $new_string[$i] = trim($new_string[$i]);
    $val = explode(" ", $new_string[$i]);
   
echo '<h3>' . $i .'.</h3>';
echo $key . " ";
echo $val[0] . " ";
echo $val[1] . " ";
echo $val[2] . " ";
echo $val[3] . "<br />";
}


with your last modification
Code:

for ($i = 1; $i <= $count-1; $i++) {
    $new_string[$i] = trim($new_string[$i]);
    $val = explode(" ", $new_string[$i]);
    $r=$i-1;   
    $result[$r] = '<strong>' . $i . '. ' . $word . ': </strong>' . $val[0] . " " . $val[1] . " " . $val[2] . " " . $val[3] . "<br />";
}
foreach($result as $val) {
   echo $val;
}

But the second one is not producing any output.
PS: Now I am having two search function running in my system, search_nemesis.php and search_sonam.php, to accomodate the changes suggested by you individually.Smile
bukaida
I have made a work around with the output part

Code:

for ($i = 1; $i <= $count-1; $i++) {
    $new_string[$i] = trim($new_string[$i]);
    $val = explode(" ", $new_string[$i]);
    $newkey=$key . " ".$val[0] . " " . $val[1] . " " . $val[2] . " " . $val[3] . " ";
    echo $newkey;
}


This is producing the output. Could not do better with my limited knowledge.
Related topics
req: PHP to list files in directory, and link to them
What computer languages you know?
How did you hear about FRIHost ?
Make search engine With PHP and mySQL, for your site
design/layout ?
Any Good Script like Kings Of Chaos
HI people - Portugal in the house
can someone help me?
Creating a Search Script with PHP for MySQL
How tough is PHP?
need php script to test if domain is Blacklisted
Refine Search
php/sql question
PHP website with SQL Database.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.