Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/185753-how-to-ignore-lowercase-items/
Share on other sites

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

 

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?

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);

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.