Paul Dessert
Paul Dessert

Reputation: 6389

Searching for words in a string

What's the best way to search a string in php and find a case insensitive match?

For example:

$SearchString = "This is a test";

From this string, I want to find the word test, or TEST or Test.

Thanks!

EDIT

I should also mention that I want to search the string and if it contains any of the words in my blacklist array, stop processing it. So an exact match of "Test" is important, however, the case is not

Upvotes: 1

Views: 225

Answers (5)

Puggan Se
Puggan Se

Reputation: 5846

If you want to find word, and want to forbid "FU" but not "fun", you can use regularexpresions whit \b, where \b marks the starts and ends of words, so if you search for "\bfu\b" if not going to match "fun", if you add a "i" behind the delimiter, its search case insesitive, if you got a list of word like "fu" "foo" "bar" your pattern can look like: "#\b(fu|foo|bar)\b#i", or you can use a variable:

if(preg_match("#\b{$needle}\b#i", $haystack))
{
   return FALSE;
}

Edit, added multiword example whit char escaping as requested in comments:

/* load the list somewhere */
$stopWords = array( "word1", "word2" );

/* escape special characters */
foreach($stopWords as $row_nr => $current_word)
{
    $stopWords[$row_nr] = addcslashes($current_word, '[\^$.|?*+()');
}

/* create a pattern of all words (using @ insted of # as # can be used in urls) */
$pattern = "@\b(" . implode('|', $stopWords) . ")\b@";

/* execute the search */
if(!preg_match($pattern, $images))
{
    /* no stop words */
}

Upvotes: 2

xobicvap
xobicvap

Reputation: 302

I wasn't reading the question properly. As stated in other answers, stripos or a preg_match function will do exactly what you're looking for.

I originally offered the stristr function as an answer, but you actually should NOT use this if you're just looking to find a string within another string, as it returns the rest of the string in addition to the search parameter.

Upvotes: 0

DaveRandom
DaveRandom

Reputation: 88647

You can do one of a few things, but I tend to use one of these:

You can use stripos()

if (stripos($searchString,'test') !== FALSE) {
  echo 'I found it!';
}

You can convert the string to one specific case, and search it with strpos()

if (strpos(strtolower($searchString),'test') !== FALSE) {
  echo 'I found it!';
}

I do both and have no preference - one may be more efficient than the other (I suspect the first is better) but I don't actually know.

As a couple of more horrible examples, you could:

  • Use a regex with the i modifier
  • Do if (count(explode('test',strtolower($searchString))) > 1)

Upvotes: 1

Andre Dublin
Andre Dublin

Reputation: 1158

https://www.php.net/manual/en/function.preg-match.php

Depends if you want to just match

In this case you would do:

$SearchString= "This is a test";
$pattern = '/[Test|TEST]/';
preg_match($pattern, $SearchString);

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324610

stripos, I would assume. Presumably it stops searching when it finds a match, and I guess internally it converts to lower (or upper) case, so that's about as good as you'll get.

Upvotes: 0

Related Questions