How can multiple non-contiguous word parts be found in a PHP search?

When searching for multiple non-contiguous word parts in PHP, you can use regular expressions to match the individual word parts in any order within the search string. By using the preg_match function with a regex pattern that includes all the word parts separated by a wildcard character, you can efficiently search for the desired terms.

$searchString = "This is a sample search string";
$wordParts = ["sample", "string"];

$regexPattern = "/.*" . implode(".*", $wordParts) . ".*/";

if (preg_match($regexPattern, $searchString)) {
    echo "Match found!";
} else {
    echo "No match found.";
}