-2

Here's what I'm running:

echo $checknetworks;

Here's the results of the echo:

Facebook,Twitter,Myspace,Google,Instagram,Pinterest

What I want to do is check to see if the string google is in the results above. I do not want it to be case sensitive because the capitalization changes from time to time.

Basically if google exists in the string, I want to display "FOUND". If it doesn't exist, I want to display "NOT FOUND".

I came across a couple of somewhat similar questions here but none seemed to take capitalization into account.

user10848
  • 161
  • 1
  • 8
  • Please refer to http://stackoverflow.com/questions/4366730/check-if-string-contains-specific-words – Panda Jan 23 '16 at 16:52
  • 1
    [php:stripos](http://php.net/manual/en/function.stripos.php) – Franz Gleichmann Jan 23 '16 at 16:53
  • There is `stristr()` that is case insensitive and you could also use `explode()` and loop through the array to see if *google* is found but then you need to convert the `strtolower()` But we are not here to write you a code, show us what you have instead of only displaying the output. – Xorifelse Jan 23 '16 at 19:36

2 Answers2

1

You need stripos:

stripos — Find the position of the first occurrence of a case-insensitive substring in a string

$checknetworks = "Facebook,Twitter,Myspace,Google,Instagram,Pinterest";

if (stripos($checknetworks, 'Google') === FALSE)
{
    echo 'NOT FOUND';
} else
{
    echo 'FOUND';
}

Please note that you should compare types as well. I.e. if your string would start with google, stripos will return 0, that would be interpreted as false, unless you make the type comparison with ===

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
0

try using strpos:

<?php
$strVar = (string)$myVar;
if (strpos($strVar, "Google")){
    echo "Found"
}else{
    echo "Not found"
}
?>

EDIT:

You must check if the strpos returns FALSE, and not the position 0. Use '===':

if (strpos($strVar, "Google") === FALSE){
Joas
  • 11
  • 5
  • if `Google` is the first occurrence in the string like `Google,Facebook,Pinterest`, your solution will return `false`. Because you're comparing the result only, not types... – Kevin Kopf Jan 23 '16 at 17:04