N-Bomb(Nerd) Posted March 25, 2009 Share Posted March 25, 2009 I need some help filtering input from file1.txt to file2.txt. I've been trying for about the last 6-7 hours to make this script and failing horribly. What I'm looking to have my script do is open file1.txt and remove all non-alpha characters. Then as it's writing to file2.txt it will replace all spaces with newlines.. Example: File1.txt reads: Th1e do2dgers wo4n t3he b6ase2ba43ll ga#%!me! After the script is ran File2.txt will look like this: The dodgers won the baseball game Any help is appreciated, thanks. Link to comment https://forums.phpfreaks.com/topic/151065-filtering-input/ Share on other sites More sharing options...
.josh Posted March 25, 2009 Share Posted March 25, 2009 $string = preg_replace('~[^a-z ]~i','',$string); $string = str_replace(' ',"\n",$string); Link to comment https://forums.phpfreaks.com/topic/151065-filtering-input/#findComment-793593 Share on other sites More sharing options...
POG1 Posted March 25, 2009 Share Posted March 25, 2009 You won't want to remove things as it may do things to the file, why not just only accept it if it contains the alphabetic chars? http://uk2.php.net/manual/en/function.ctype-alpha.php Link to comment https://forums.phpfreaks.com/topic/151065-filtering-input/#findComment-793723 Share on other sites More sharing options...
.josh Posted March 25, 2009 Share Posted March 25, 2009 You won't want to remove things as it may do things to the file, why not just only accept it if it contains the alphabetic chars? http://uk2.php.net/manual/en/function.ctype-alpha.php It's a text file. The only thing special about a text file is whether there are end of lines in there or not. The worst that can happen is if you explicitly remove them, you won't have stuff on their own lines anymore. The code I provided doesn't touch what may or may not already be there. In fact if anything, maybe it should, since he wants the end product to be a list, and any new lines originally there would create blank lines. Link to comment https://forums.phpfreaks.com/topic/151065-filtering-input/#findComment-793798 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.