Twango Posted April 4, 2011 Share Posted April 4, 2011 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 More sharing options...
jeffman Posted April 4, 2011 Share Posted April 4, 2011 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 More sharing options...
Twango Posted April 5, 2011 Author Share Posted April 5, 2011 OK I'll try it! thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.