Find matching words (two strings)

I’ve got a file $datafile(‘spamwords’) that has spam words / phrases in it:
gambling
xxx
milf
porn
adult movies
cbd shops
etc…
SEMrush

I want to compare the spamwords file with another file: $text.

I found this code, but it doesn’t really do the job. I don’t get why it has this part

$a1=explode(‘ ‘,$str1);
$a2=explode(‘ ‘,$str2);

The code does find some spam words in the $text string ($str2), but not all of them, which is strange.


$spamwords = file_get_contents($datafile('spamwords'));   
$text = "Some text that has spam words in it. Can be whatever text.";
   
$str1=$spamwords;
$str2=strtolower($text);
$a1=explode(' ',$str1);
$a2=explode(' ',$str2);
function longenough($word){
    return strlen( $word ) > 3;
}
$a1=array_filter($a1,'longenough');
$a2=array_filter($a2,'longenough');
$common=array_intersect( $a1, $a2 );
foreach( $common as $word ){
    $str2=preg_replace( "/($word)/i",'<span style="color:red;font-weight:bold;">$1</span>', $str2 );
}
echo $str2;

Code (markup):

Thank you.