Replacing ereg_replace now that it is deprecated

45 Views Asked by At

In the following lines of code, how can I convert ereg_replace to preg_replace, now that ereg_replace is deprecated.

I have a file called badwords.txt which contains a long range of phrases / words (one per line) which are likely to be spam (these could contain any characters). I use this code to calculate the number of likely-to-be-spam words and then decide what to do with the referrer.

$fh = fopen("badwords.txt","r"); //Open the file that contains the words
while($word = fgets($fh,4096))
{
$text = ereg_replace(trim($word),"#####",$text); 
}
$offence = substr_count($text, "#####");

I get that if it was:

$text = ereg_replace('[^A-Za-z0-9_]', '######', $text );

then it would become:

$text = preg_replace('/[^A-Za-z0-9_]/', '######', $text );

However, I can't make the extra escapes work with my code above. When I run the ereg_replace code I get an error but the data displays nonetheless. When I switch it to preg_replace(trim($word),"#####",$text); nothing shows.

Grateful for any pointers!

0

There are 0 best solutions below