0

If you have

$str1 = "h*llo";
$str2 = "hello";

is it possible to quickly compare them without first having to remove the * from str1, and the matching indexed character from str2?

The solution would need to work regardless of how many * are in the string, eg:

$str1 = "h*l*o";
$str2 = "hello";

Thanks for taking a look.

Shaun
  • 2,043
  • 3
  • 27
  • 36
  • 1
    You are looking for [`fnmatch()`](http://php.net/fnmatch). – mario Oct 15 '13 at 03:17
  • possible duplicate of [php string matching with wildcard \*?](http://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard) – mario Oct 15 '13 at 03:19
  • @mario Didn't know about that function! Probably a more complete/faster/better overall method than using regular expressions. – John V. Oct 15 '13 at 03:19
  • Thank guys - fnmatch examples don't seem to be working. I'll check if it's available on my server. – Shaun Oct 15 '13 at 06:51

2 Answers2

3

Yes, with regex, and more specifically preg_match for PHP. What you are looking for are "wildcards".

This is untested but should work for you:

$str1 = "h*llo";
$str2 = "hello";

//periods are a wildcards in regex
if(preg_match("/" . preg_quote(str_replace("*", ".*", $str1), "/") . "/", $str2)){
    echo "Match!";
} else {
    echo "No match";
}

EDIT: This should work for your case:

$str1 = "M<ter";
$str2 = "Moter";

//periods are a wildcards in regex
if(preg_match("/" . str_replace("\<", ".*", preg_quote($str1, "/")) . "/", $str2)){
    echo "Match!";
} else {
    echo "No match";
}
John V.
  • 4,652
  • 4
  • 26
  • 26
  • 1
    That's what I'd prefer still. +1 for thinking about preg_quote. Though replace `*` with `.*` to match multiple chars. – mario Oct 15 '13 at 03:23
  • Thanks for the `.*` suggestion, I'm not that great at regex. – John V. Oct 15 '13 at 03:24
  • Perhaps I should have not used the wildcard symbol. My text has the character "<" in the place of any punctuation and accents, and now I need to compare the strings. eg "M – Shaun Oct 15 '13 at 06:59
  • @Shaun Just change `str_replace("*"` to `str_replace("<"` – John V. Oct 15 '13 at 07:14
  • Hi John. The example you've given gives "no match". Should it be a match? – Shaun Oct 15 '13 at 07:48
  • To be sure, I'll update the question with the code that is relevant to your `<` case. – John V. Oct 15 '13 at 10:14
  • Thank you John. That one displays "No match" too - is it supposed to display "Match!"? – Shaun Oct 15 '13 at 14:45
  • WOWWWWWW!!! Your code sped the processing time up from an average of 0.0034 seconds per record to 0.00053 over the method I was using!!!!! Cheers mate!!!! – Shaun Oct 16 '13 at 11:50
  • @Shaun I'm glad I could help! Make sure that if you think this is the correct answer to your question you click the green check mark to the left of it to mark it as "Accepted". – John V. Oct 16 '13 at 14:03
1

You can use similar_text() to compare two strings and accept if the result is above e.g. 80%.

 similar_text($str1, $str2, $percent); 

Example:

$str1 = 'AAA';
$str1 = '99999';

similar_text($str1, $str2, $percent); 
echo $percent; // e.g. 0.000

$str1 = "h*llo";
$str2 = "hello";

similar_text($str1, $str2, $percent); 
echo $percent; // e.g. 95.000

See more here PHP SIMILAR TEXT

CIRCLE
  • 4,501
  • 5
  • 37
  • 56