Jump to content

selecting words out of an array?


spires

Recommended Posts

Hi Guys.

 

Is it posible to select a unique word in an array, and display all the words the has that words in. 

 

Example:

 

Array list:

pink blackberry

blackberry case

blackberry phone

blackberry 8800 pink

headset case

 

To be selected list:

pink

case

 

Results:

pink blackberry

blackberry 8800 pink

blackberry case

headset case

 

All of the pinks appear, then all of the caes appear.

 

So far I have this (both arrays)

but i need to know how to select them.

		$list = $_POST['list'];
		$list_array = explode("\n", $list);

		$cat = $_POST['cat'];
		$cat_array = explode("\n", $cat);



	foreach ($list_array as $lists) {
	foreach ($cat_array as $cats) {
                $lists = trim($lists);
			$cats = trim($cats);

         }

         }
}

 

Thanks for your help

 

Link to comment
Share on other sites

I'm not sure what you mean "select them".

 

The easiest thing would be in your first foreach to do something like:

 

if (strpos($lists,"pink" > 0)
   echo $lists

 

And then do something similar for the second loop. I am sorta new to this too, but the strpos would be a way to check for the substring within the string, yeah?

Link to comment
Share on other sites

It's a good idea, but I can't seem to get it to work.

 

The problem is that out of 100 words, 10 will have the word pink in.

So I need to display that batch together.

 

I'm sure that there is an easy way.

 

I just have to find it.

 

Cheers

Link to comment
Share on other sites

<?php

function match_categories($list, $cat) {

    //Create the arrays
    $list_array = explode("\n", $list);
    $cat_array = explode("\n", $cat);

    //clean the data
    foreach ($list_array as $key => $value) $list_array[$key] = trim($value);
    foreach ($cat_array as $key => $value) $cat_array = trim($value);

    $return_vals = array();

    //Search each category for each list item
    foreach ($cat_array as $category) {

        foreach ($list_array as $list_item) {

            if (strpos($category,$list_item)) {
                $return_vals[$list_item] = $category;
            }
        }
    }
}

$found_categories = match_categories($_POST['list'], $_POST['cat'])

?>

Link to comment
Share on other sites

or try

<?php
$a =
'pink blackberry
blackberry case
blackberry phone
blackberry 8800 pink
headset case';
$c = 
'pink
case';
$c = str_replace(' ','',$c);
$c = str_replace("\n",'|',$c);
echo $out = preg_replace("/(^.*(\b($c)\b).*$)|(^.*\n?\r?)/m",'$1',$a);
?>

Link to comment
Share on other sites

Came up with this one.

using preg_match.

also added a simple scoring system, which than sorts the matches by score.

 

<?php
$list=array('pink blackberry','blackberry case','pink blackberry case','blackberry phone','blackberry 8800 pink','headset case');

$search=array('pink','case');

function rsearch($list,$keys)
{
$scores=array();
foreach($keys as $term)
{
	$term=preg_quote($term,'/');
	foreach($list as $key=>$slist)
	{
		$score=(preg_match("/^$term\$/i",$slist)*1000) + // complete match  highest score
			   (preg_match("/\b$term\b/i",$slist)*100) + // Within search list, Med Score
				preg_match("/$term/i",$slist);           // compounded search term, low score
		if($score) $scores[$key]+=$score;
	}
}
$keys=array_keys($scores);
$items=count($keys);
for($i=0;$i<($items-1);$i++)
{
	for($j=1;$j<$items;$j++)
	{
		if($scores[$keys[$i]]<$scores[$keys[$j]])
			$keys[$j] ^= $keys[$i] ^= $keys[$j] ^= $keys[i];
	}
}
foreach($keys as $key)
	$matches[]=$list[$key];
return $matches;

}

header('Content-type: text/plain');
print_r(rsearch($list,$search));

?>

 

 

U can prolly add a lot more to the scoring system. maybe adding weights to the search terms position.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.