Following php code checks to find substring in string:
1. Simple Case
<?php $a = 'How are you?'; if (strpos($a, 'are') !== false) { echo 'true'; } ?>
2. Check if a string contains a word from an array
function match_found($needles, $haystack) { foreach($needles as $needle) { if (strpos($haystack, $needle) !== false) { return true; } } return false; } // Test $needles = array('computer','phone'); $haystack = 'I have a phone'; if(match_found($needles, $haystack)){ echo "Match found."; }
3. Check if a string contains all words in an array
function match_all_found($needles, $haystack) { if(empty($needles)) { return false; } foreach($needles as $needle) { if (strpos($haystack, $needle) == false) { return false; } } return true; } // Test $needles = array('computer','phone'); $haystack = 'I have a computer and a phone'; if(match_all_found($needles, $haystack)){ echo "Match(es) found."; }
Javascript:
var str = "foo"; var sub = "oo"; if (str.search(sub) != -1) { console.log(true); } else { console.log(false); }