-3

I want to saerch for a special string in a string but the script should ignore capitals.

Example code:

if (in_string($string, "stringINaSTRING")) {
    echo "The String is in the string!";
}

If $string contains STRINGINASCTRING it should echo The String is in the string.

How can I get this done?

demongolem
  • 8,796
  • 36
  • 82
  • 101
Bernd
  • 155
  • 7
  • 1
    Or convert each string to lowercase and then compare. – Sumner Evans Sep 08 '15 at 19:32
  • If you got that `in_string` function from the http://php.net/manual/en/function.strpos.php page there is an option for case sensitivity. `function in_string($needle, $haystack, $insensitive = false)` – chris85 Sep 08 '15 at 19:35

3 Answers3

2

Use a case insensitive search like stripos()

if (stripos($string, "STRINGINASTRING"))
{
    echo "The String is in the string!";
}
asdf
  • 2,608
  • 2
  • 12
  • 40
  • strpos() would be faster. – devlin carnate Sep 08 '15 at 19:37
  • @devlincarnate I doubt `strpos()` would be faster because you'd have to call `strtolower()` on your search string. However `stripos()` is definitely faster. – asdf Sep 08 '15 at 19:39
  • 1
    ah, ok, you're right. slightly faster, but faster is faster: http://www.spudsdesign.com/index.php?t=strpos6 – devlin carnate Sep 08 '15 at 19:43
  • @devlincarnate Wow that's a lot closer than I was expecting. I ran it a few more times and it went back and forth in which one came out faster. I bet `stripos()` is doing something similar to `strpos()/strtolower()` under the hood – asdf Sep 08 '15 at 19:46
1

In the past, I've accomplished this (in all sorts of different languages) by converting your string variable to upper or lower case and comparing against an uppercase or lowercase string literal. That is to say:

if (strpos(strtoupper($string), "STRINGINASTRING") !== false) {
    echo "The String is in the string!";
}  

Also, note the use of the strpos function instead of your in_string declaration.

This methodology only calls the string conversion function once, since you already know the literal string you should be comparing your variable to, you can simply define it yourself as all uppercase or all lowercase (used with strtolower); take your pick.

One of the niceties behind this concept is it is applicable in languages that don't have case insensitive functions. Seems more universal to me; but on the other hand, having functions to do the same job is quiet convenient...

sadmicrowave
  • 35,768
  • 34
  • 99
  • 172
  • what if the search string is at the beginning of the haystack? strpos will return 0, which would fail your comparison. if(strpos($haystack,$needle) !== false) is a better method. – devlin carnate Sep 08 '15 at 19:40
  • @devlincarnate agreed, updated answer. Similarly, `>= 0` would also work instead of `!== false` – sadmicrowave Sep 08 '15 at 19:44
  • @devlincarnate that is why you should use `===` instead of `==` if string is not found it will return false and `0 !== false` is false but `0 != false` is true – Robert Sep 08 '15 at 20:31
  • @Robert: the answer originally had if(strpos($haystack,$needle) { //found } , which is what my comment was directed toward. That answer has since been updated/edited. – devlin carnate Sep 08 '15 at 20:52
  • okay I did not know that – Robert Sep 08 '15 at 22:06
0

Convert everything to lower

if (in_string(strtolower($string), strtolower("stringINaSTRING"))) {
  echo "The String is in the string!";
}
Jason K
  • 1,386
  • 1
  • 9
  • 15