Search This Blog

Loading...

Sunday, December 11, 2011

PHP - Check if a string contains a substring

/**
* Checks to see of a string contains a particular substring
* @param $substring the substring to match
* @param $string the string to search 
* @return true if $substring is found in $string, false otherwise
*/
function contains($substring, $string) {
        $pos = strpos($string, $substring);
 
        if($pos === false) {
                // string needle NOT found in haystack
                return false;
        }
        else {
                // string needle found in haystack
                return true;
        }
 
}


http://www.thetechrepo.com/main-articles/451-php-check-if-a-string-contains-a-substring

0 comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails