Jump to content

Closeness of match


griffen

Recommended Posts

I have an array containing variations of names so something like:
flext35 web n' walk| flext 35 web and walk|flext35 web and walk|flext35 + web and walk

I have a name then which I use a haystack and then carry out a search using the variations as patterns.

The problem is that if I get a haystack like:
flext35 + web and walk

and my pattern is:
flext 35|flext35

it will get a match on the haystack above when in actual fact I want the search to continue and use the pattern in the example above.

Does anyone know how to get a closeness of match?

I know this is search engine like territory but surely there must be some easish way of doing this.

Thanks.
Link to comment
Share on other sites

I've got an array containing products. Each product contains a variations element for a tariff. This will look something like Flext35|Flext 35

That way the variations can be used in regexp.

The next thing is I have a CSV spreadsheet that contains tariff's. I then run through the product array checking the variations against the tariff that I have just read. The problem is that in some situations I have:
- Flext 35
- Flext 35 + Web 'N Walk

The issue here is that if the current tariff is "Flext 35 + Web 'N Walk" and I have read in Flext 35 it will tell me that I have a match when actually I want my for loop to scroll a little further and find the next one.

Hence my question. Is there someway of getting a closeness of match rating?
Link to comment
Share on other sites

The problem you're having (I think) is the alternation in your regex.
This:[code](flext35 web n' walk| flext 35 web and walk|flext35 web and walk|flext35 + web and walk)[/code]
As I'm sure you're well aware is a logical OR statement. So as soon as one of those patterns is matched, it becomes true and the rest are skipped (This is because PHP uses a NFA regex engine, but that's a little beyond the issue at hand). Something like this may be more suitable to your application:
[code]if(preg_match('/flex\s?35/i', $string) && preg_match('/web (?:n\'|and) walk/i', $string)) {
  $both++; // Do whatever you want to do if both are set
}else{
  if(preg_match('/flex\s?35/i', $string)) {
      $count++;
      // Or whatever else you want to do if "flex 35" is set
  }
  if(preg_match('/web (?:n\'|and) walk/i', $string)) {
      $count++;
      // Or whatever else you want to do if "Web and Walk" is set
  }
}[/code]Test each condition and do what you want based on what comes of the logical testing. (You could combine the logical AND in the first 'if' into the regex, but it sounds like your data isn't very consistent. For that reason, I'd leave them separated, it'll be easier to debug later)
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.