How can you search for a specific string within a larger text using preg_match_all in PHP?

To search for a specific string within a larger text using preg_match_all in PHP, you can use a regular expression pattern to match the string you are looking for. You can then use the preg_match_all function to search for all occurrences of the pattern within the text.

$text = "This is a sample text with the word 'sample' appearing multiple times.";
$search_string = 'sample';

preg_match_all("/$search_string/", $text, $matches);

print_r($matches[0]);