How can PHP be used to search for parts of words or incomplete words in a text?

When searching for parts of words or incomplete words in a text using PHP, we can utilize regular expressions to match the desired patterns. By using the preg_match() function along with a regular expression pattern, we can search for substrings within a text that may not be complete words. This allows for more flexible and inclusive search functionality.

$text = "This is a sample text where we will search for parts of words.";
$searchTerm = "sam"; // Search term to look for

if (preg_match("/\b" . $searchTerm . "\b/i", $text)) {
    echo "Found match for '" . $searchTerm . "' in the text.";
} else {
    echo "No match found for '" . $searchTerm . "' in the text.";
}