Jump to content

Comparing 2 strings or a string with an array


Hop_Hop

Recommended Posts

hello,

I want to make a product name comparison script and i have big difficulties at comparing a product name from a file with more product names from the dbase.

 

i have the folowing product name:

$string = "GSM Phone C330";

and i want to compare it with more product names from my dbase based on word matching. Let`s say if $string has 2 words that are simmilar with 2 words from a productname from my dbase it`s true.

 

My analogy was : $string with array from db, but i`m stuck at splitting in words and comparing them.

 

Can anyone help me? i`m stuck.

 

thanks

Not sure if I fully understand what you want, but:

 

You could explode the string based on spaces:

 

$string="GSM Phone C330";

$words=explode(" ", $string); // $words = array("GSM", "Phone", "C330")

 

From there you could loop through each word and run it to the database. That would give you results for one word.

 

For two words, you could have it run through a loop and combine the various combinations of words:

for ($x=0; $x<=count($words); $x++)
{
$myWord1=$words[$x];

for ($y=$x; $y<=count($words); $y++)
  {
  $myWord2=$words[$y];
  }

$sql="SELECT * FROM `table_name` WHERE `FIELD` LIKE %{$myWord1}% AND `FIELD` LIKE %{$myWord2}%";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.