2

I have the following function to do a "exact match" of a pattern($searchPat) in a sentence ($sourceStr)

function isUsed($sourceStr, $searchPat) {
 if (strpos($sourceStr, $searchPat) !== false) {
    return true;
    } else {
    return false;
    }
}

However, this doesn't do an exact match. I changed the function as follows but this doesn't even execute.

function isUsed($sourceStr, $searchPat) {
if (preg_match("~\b[$sourceStr]\b~", $searchPat)) {
    return true;
    } else {
    return false;
    }
}

How could I do an exact match please?

chris85
  • 23,846
  • 7
  • 34
  • 51
usert4jju7
  • 1,653
  • 3
  • 27
  • 59
  • 2
    Take out the `[]`, those list allowed characters. Is `$sourceStr` the sentence to search or the term to find? – chris85 Feb 09 '16 at 05:22
  • Hello Chris - Thank you. Your understanding is correct. `$sourceStr` is the sentence to search in while `$searchPat` is the item to search for. – usert4jju7 Feb 09 '16 at 05:24
  • You can use stripos () too. –  Feb 09 '16 at 05:26
  • Hello Devashish - Thank you. As I've mentioned int he question, `strpos` isn't doing an exact match. Could you please help me with the modified code to do this? – usert4jju7 Feb 09 '16 at 05:28
  • `stripos` would return false positives. For example if searching for `bee` the word `been` would match. – chris85 Feb 09 '16 at 05:28
  • this questions seems to be a duplicate of [this](http://stackoverflow.com/questions/1019169/how-can-i-check-if-a-word-is-contained-in-another-string-using-php) and [this](http://stackoverflow.com/questions/4366730/check-if-string-contains-specific-words) – jameshwart lopez Feb 09 '16 at 05:28

4 Answers4

5

The [] is a character class. That lists characters you want to allow, for example [aeiou] would allow a vowel. Your variables are also in the inverted order, pattern first, then string to match against. Try this:

function isUsed($sourceStr, $searchPat) {
     if (preg_match("~\b$searchPat\b~", $sourceStr)) {
         return true;
     } else {
         return false;
     }
}

Additional notes, this is case sensitive, so Be won't match be. If the values you are passing in are going to have special characters the preg_quote function should be used, preg_quote($variable, '~'). You also may want to concatenate the variable so it is clear that that is a variable and not part of the regex. The $ in regex means the end of the string.

chris85
  • 23,846
  • 7
  • 34
  • 51
2

Try This.

function isUsed($sourceStr, $searchPat) {
if (preg_match("/\b".preg_quote($sourceStr)."\b/i", $searchPat)) {
    return true;
    } else {
    return false;
    }
}
0

Please try "preg_match" for matches.

$string = 'test';
if ( preg_match("~\btest\b~",$string) )
  echo "matched";
else
  echo "no match";

Or try like this

if(stripos($text,$word) !== false) 
      echo "no match";
    else
      echo "match";
Jalpa
  • 697
  • 3
  • 13
0

You can try this one:

  function isUsed($string_to_search, $source_String) {
    if (preg_match("/$string_to_search/", $source_String)) {
    return true;
    } else {
     return false;
    }
 }

You can change according to your need.

Case insensitive: preg_match("/$string_to_search/i", $source_String)
Boundry condition: preg_match("/\b$string_to_search\b/i", $source_String)
Special characters: if you have any special characters in your string for your safe side replace it with '\special_character'

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42