mikeee Posted December 20, 2009 Share Posted December 20, 2009 Hello, I have a script that grabs info from a text file and inserts it into a DB. The text file contains pipe delimited information like so... AWL | 23 | 5656 | 335 BMG | 16 | 52 | 4025 awl | 444 | 60 | 81048 etc... The letter in the first field are unique. However, at the very end of the file I have lines with the same letters but in lowercase but with different data in the additional fields. I need to ignore those lines because PHP inserts the first line with CAPS and then overrides it with the lowercase line. Is there a way to ignore the lower case line? My code to read the file is this: $file=file("/home/var/public_html/load/file.txt"); foreach($file as $linex) { $line=explode('|',$linex); $line=array_map('trim',$line); $line=array_map('mysql_real_escape_string',$line); $query="UPDATE users SET ........."; } Any direction would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/185753-how-to-ignore-lowercase-items/ Share on other sites More sharing options...
teamatomic Posted December 20, 2009 Share Posted December 20, 2009 ctype_upper($str) ctype_lower($str) These return true or false so you can easily use this: ctype_upper("$str") ? "echo 'is upper';" : "echo 'is lower';"; One thing. ALL the characters in the string need to be that same case or you will always get false. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/185753-how-to-ignore-lowercase-items/#findComment-980833 Share on other sites More sharing options...
mikeee Posted December 20, 2009 Author Share Posted December 20, 2009 ctype_upper($str) ctype_lower($str) These return true or false so you can easily use this: ctype_upper("$str") ? "echo 'is upper';" : "echo 'is lower';"; One thing. ALL the characters in the string need to be that same case or you will always get false. HTH Teamatomic Thanks for the quick reply. I have a question, please excuse my ignorance as I am still a PHP newbie... Your example says to echo. But I dont want to output anything. So how do I use your example in my query? Is there a way to insert a code before the query to ignore the lowercase line? Quote Link to comment https://forums.phpfreaks.com/topic/185753-how-to-ignore-lowercase-items/#findComment-980834 Share on other sites More sharing options...
Zane Posted December 20, 2009 Share Posted December 20, 2009 Something like this should do it. $line=explode('|',$linex); $line = (ctype_upper($line[0])) ? $line : continue 0; // After you've exploded them you can check the first key. If it fails.. go to the next loop. $line=array_map('trim',$line); $line=array_map('mysql_real_escape_string',$line); Quote Link to comment https://forums.phpfreaks.com/topic/185753-how-to-ignore-lowercase-items/#findComment-980839 Share on other sites More sharing options...
mikeee Posted December 20, 2009 Author Share Posted December 20, 2009 Something like this should do it. $line=explode('|',$linex); $line = (ctype_upper($line[0])) ? $line : continue 0; // After you've exploded them you can check the first key. If it fails.. go to the next loop. $line=array_map('trim',$line); $line=array_map('mysql_real_escape_string',$line); Thank you. Any idea why I would get Parse error: syntax error, unexpected T_CONTINUE Quote Link to comment https://forums.phpfreaks.com/topic/185753-how-to-ignore-lowercase-items/#findComment-980840 Share on other sites More sharing options...
teamatomic Posted December 20, 2009 Share Posted December 20, 2009 The error is because continue cant be use in a construct like that. It is made to move farther down or out in looping. What you need to do is use ctype_upper($line[0] in an if statement right after you explode the stuff into an array. if (ctype_upper($line[0]) {keep value or swap} If you want to keep the value or swap them then do it, if you want to leave then use an break; or an else{break;}. I would explode them into vars myself: list($var1,$var2,$var3,$var4)=explode("|","$linex), it makes it easier to read and keep track of what you are doing. But thats just my prefered way of doing it. If the results you are getting are not what you expect then try negating the if statement !ctype_upper($line[0]) HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/185753-how-to-ignore-lowercase-items/#findComment-980851 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.