point86 Posted March 7, 2007 Share Posted March 7, 2007 Hi, If I have read in a file, which I then convert into a string (say "string1"), how do I make a loop that goes through the string, makes all the letters capitals and writes all the LETTERS (not spaces, periods etc) into a new string ("string2")? Thanks Point. Quote Link to comment Share on other sites More sharing options...
jcbarr Posted March 7, 2007 Share Posted March 7, 2007 <?php $string = strtoupper($string); ?> That will convert everything to upper case. Then you can use a replace function to remove any non letter characters, I'm not the right one to show you how to do that because I'm not an expert on regular expressions yet. But look in to preg_replace Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 7, 2007 Share Posted March 7, 2007 i think this is what you want: <?php function convert($file){ $fp = fopen($file, 'r') while(!feof($fp)){ $line = fgets($fp, 1024); $line = strtoupper($line); $line = str_replace(" ", "", $line); $line = str_replace(".", "", $line); $string[] = $line; } fclose($fh); return $newstring = explode("\n", $string); } /*execute function*/ convert("your_file.txt"); ?> Quote Link to comment Share on other sites More sharing options...
point86 Posted March 7, 2007 Author Share Posted March 7, 2007 That's great, cheers. Point. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 7, 2007 Share Posted March 7, 2007 That's great, cheers. Point. glad it worked! my code was untested =P Quote Link to comment Share on other sites More sharing options...
effigy Posted March 7, 2007 Share Posted March 7, 2007 Beware that it doesn't cover the etc part... LETTERS (not spaces, periods etc) If you only want letters, you'll need a regular expression. You'll also need to use Unicode properties if there are letters outside of the A-z range. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 7, 2007 Share Posted March 7, 2007 Beware that it doesn't cover the etc part... LETTERS (not spaces, periods etc) If you only want letters, you'll need a regular expression. You'll also need to use Unicode properties if there are letters outside of the A-z range. i definitely would have used preg_replace() instead of a couple of str_replace()'s, but unfortunately i am unfamiliar with regex =\. i need to learn it, otherwise i'm only half as capable. do you have any suggestions for tutorials or articles or books to learn regex, effigy? Quote Link to comment Share on other sites More sharing options...
effigy Posted March 7, 2007 Share Posted March 7, 2007 Look under "Other Sources" here. Try the PHP Freaks tutorial and those @ RegularExpressions.info. If you really want to jump into regex, see the link to the book. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.