w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How to find multiple substrings of a string, but if multiple are not found, still return however many substrings were found

First, for sake of simplicity, consider those substring variables as an array. Now, the code may look like this,

function doIt($string, $sub_strings){
    foreach($sub_strings as $substr){
        if(strpos($string, $substr) !== FALSE)
        {
          return $string; // at least one of the needle strings are
substring of heystack, $string
        }
    }

   return ""; // no sub_strings is substring of $string.
}

and to use this function,

echo doIt($string,array($str1,$str2,$str3,$str4));

Thanks to Orangepill!





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.