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

Link to comment
Share on other sites

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}%";
}

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.