Forum Moderators: coopster

Message Too Old, No Replies

needle haystack searching tips

best match

         

devil_dog

4:59 am on Dec 15, 2007 (gmt 0)

10+ Year Member



php newbie here.

The thing is ... I have a paragraph or two of text leaded in a string.
I also have a list of sub directories.
Now I want to find out which sub directory name is most suitable to the content. After reading a bit i think strpos is the best way to go.

this is how far i got :-


$scanthis = '.';
$scanlisting = scandir($scanthis);
$dirlisting = array();
foreach($scanlisting as $key => $value){
if (is_dir("$scanthis/$value") == true && $value!= '.' && $value!= '..'){
$dirlisting[] = $value;
}
}

print('<ul>');
foreach($dirlisting as $key => $value){

$pos = strrpos($content, $value);
if ($pos === false) { // note: three equal signs
// not found...
} else {
print('<li><a href="' . $scanthis . '/' . $value . '">' . $value . "</a></br></li>");
}
}
print('</ul>');

So far it is diving me only an alphabetical list.

I would like to list it based on the following criteria.

1) The number of times the phrase occurs.
2) The first occurrence.

if more than 1 phrase has a tie in the first criteria, then look for the second.

How do i go about it?

Im sure it would be a piece of cake for the php gurus...

Thanks in advance.

coopster

9:16 pm on Dec 16, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would probably use an array with the directory name as the index and the value would be a counter. So, on each iteration where a word value in the phrase matches the directory listing, I would add to the counter. I hope I have understood your question correctly!