griffen Posted January 12, 2007 Share Posted January 12, 2007 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 walkI 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 walkand my pattern is:flext 35|flext35it 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. Quote Link to comment https://forums.phpfreaks.com/topic/33832-closeness-of-match/ Share on other sites More sharing options...
effigy Posted January 12, 2007 Share Posted January 12, 2007 I'm confused; can you be more detailed or show some code? See if this helps: [url=http://en.wikipedia.org/wiki/Fuzzy_string_searching]Fuzzy_string_searching[/url]. Quote Link to comment https://forums.phpfreaks.com/topic/33832-closeness-of-match/#findComment-158879 Share on other sites More sharing options...
griffen Posted January 13, 2007 Author Share Posted January 13, 2007 I've got an array containing products. Each product contains a variations element for a tariff. This will look something like Flext35|Flext 35That 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 WalkThe 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? Quote Link to comment https://forums.phpfreaks.com/topic/33832-closeness-of-match/#findComment-160013 Share on other sites More sharing options...
c4onastick Posted January 13, 2007 Share Posted January 13, 2007 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) Quote Link to comment https://forums.phpfreaks.com/topic/33832-closeness-of-match/#findComment-160024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.