Showing posts with label PHP Checking multiple words in a long string. Show all posts
Showing posts with label PHP Checking multiple words in a long string. Show all posts

Friday, 19 September 2014

PHP checking multiple words in a long one word string

<?php
$haystack = 'hihowareyoudoingtoday?';
$needles = array('hi','how','are','you');
$matches = 0;
foreach($needles as $needle) { // create a loop, foreach string
    if(strpos($haystack, $needle) !== false) { // use stripos and compare it in the parent string
        $matches++; // if it matches then increment.
    }
}

echo $matches; // 4
?>