Jump to content

Filter from txt?


Twango

Recommended Posts

How would I filter out any words that are in a text file?I use eregi_replace often but I don't know exactly how to use it in that way.

Link to comment
Share on other sites

Here's one technique. Say you have a file of forbidden words that looks like this:

fishcakesugaretc.

Read it into an array and turn the array into a string that's useful for a regex:

$words = file('words.txt', FILE_IGNORE_NEW_LINES);$re = implode('|', $words); // that's a pipe character, which will function as an OR operator$re = '/' . $re . '/';// $re now looks like this: '/fish|cake|sugar/'$str = "I like to eat my cake and fish and dog.";$str = preg_replace($re, "", $str);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...