ainoy31 Posted April 20, 2009 Share Posted April 20, 2009 I would appreciate a second eye on this. I have a text file with the following data in it: 1 Email [email protected],[email protected] 2 Email [email protected],[email protected] 3 Email [email protected] 1 Text [email protected],[email protected] 2 Text [email protected] 3 Text [email protected] I need to go through this file and pull the data out. Here is my code: $sql = "SELECT error_id FROM error_type"; $ret = mysql_query($sql); The sql gives me the error id in the table such as 1, 2, 3....etc. while($row=mysql_fetch_array($ret)) { $text = file('contactList.txt'); extract($row); foreach($text as $line) { list($error, $action, $contact) = explode(" ", $line); if($error == $error_id) { if($action == 'Email') { $email .= $contact; } if($action == 'Text') { $text .= $contact; } } } echo "Error " . $error_id . " has email addresses of: " . $email . "<br>"; echo "Error " . $error_id . " has text address of: " . $text . "<br>"; $email = ""; $text = ""; } The output is: Error 1 has email addresses of: [email protected],[email protected] Error 1 has text address of: [email protected],[email protected] Error 2 has email addresses of: [email protected],[email protected] Error 2 has text address of: [email protected] Error 3 has email addresses of: [email protected] Error 3 has text address of: [email protected] The email address output is correct but why I am getting [email protected],[email protected] for my text addresses. It should be the same as the email listings. Hope this is clear enough. Much appreciation. AM Quote Link to comment https://forums.phpfreaks.com/topic/154931-solved-read-data-from-a-text-file/ Share on other sites More sharing options...
soak Posted April 20, 2009 Share Posted April 20, 2009 You're using the variable $text as an array and also as a string to store the results to echo later. Rename one of them. Quote Link to comment https://forums.phpfreaks.com/topic/154931-solved-read-data-from-a-text-file/#findComment-814926 Share on other sites More sharing options...
laffin Posted April 20, 2009 Share Posted April 20, 2009 I think this might be better suited for file/foreach/preg_match code the pattern for preg match should be something like (\d+)\s(\w+)\s(\w+@[\w\.]+),?(\w+@[\w\.]+)? but may require tweaking Quote Link to comment https://forums.phpfreaks.com/topic/154931-solved-read-data-from-a-text-file/#findComment-814938 Share on other sites More sharing options...
ainoy31 Posted April 21, 2009 Author Share Posted April 21, 2009 "You're using the variable $text as an array and also as a string to store the results to echo later. Rename one of them. " Thanks for the second eye Soak. That fixed my issue. Later. AM Quote Link to comment https://forums.phpfreaks.com/topic/154931-solved-read-data-from-a-text-file/#findComment-815172 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.