Jump to content

Creating array from a txt file - Need Help


tartou2

Recommended Posts

Hello

I was trying from days to create an array from a txt file by i can't make it happen. I am so desperate now and i want your help guys

 

The code create a txt file from a textarea feild after it's submited each on a line. After that the php code need collect these data from the txt file and create an array from them.

 

Example: if i type in the textarea:

test

test1

test2

 

I want these usernames to be inserted in the array so it would be like this:

$names = array('test','test1','test2');

 

Anyone can help me  :confused:

 

This is the code


<html>
  <head>
   <title></title>
   <style type="text/css">
    body{
    background-color:#1166aa;
    }
   #mainbox{
   width:inherit;
   background-color:#FFFFFF;
   padding:14px;
   border:solid 3px #000000;
   float:left;
   }
   #mainbox2{
   background-color:#CCCCCC;
   border:solid 2px #000000;
   padding:11px;
   }
   #contaner{
   border:solid 0px #00FF00;
   }
   h1:hover{
   color:#0000CC;
   cursor:pointer;
   }
   #msg{
   cursor:pointer;
   }
   table td{
   border-top:outset 1px #999999;
   }
   #amount{
   color:#FF0000;
   cursor:crosshair;
   }
   #to{
   color:#3300FF;
   }
   #from{
   color:#3300FF;
   }
   #subject{
   color:#3300FF;
   }
   </style>
  </head>

<body alink="#003399" vlink="#003399">


<?php
/////////////////////////////////////////////////
function create_list(){

     $list= ($_GET ['name_list']);
     $file= "list.txt"; 
     $file_pointer = fopen($file ,'w');
     fwrite($file_pointer , $list);
     fclose($file_pointer);
     }
                
//////////////////////////////////////////////////
     $list= "";
 create_list();
//////////////////////////////////////////////////
function explodeRows($data) {
  $rowsArr = explode("\n", $data);
  return $rowsArr;
}

$filename = "list.txt";
$handle   = fopen($filename, 'r');
$data     = fread($handle, filesize($filename));
$rowsArr  = explodeRows($data);

for($i=0;$i<count($rowsArr);$i++) {
  $name = explodeRows($rowsArr[$i]);



function name(){



$names = array('the usernames submited to be inserted here','the usernames submited to be inserted here',.......);



}

}
////////////////////////////////////////////////////

if ($_GET ['submit'] == "submit"){
     create_list();
 name();
 }


?>
<script type="text/javascript">
function foo(textarea,limit){
var val=textarea.value.replace(/\r/g,'').split('\n');
if(val.length>limit){
alert('You can not enter\nmore than '+limit+' lines');
textarea.value=val.slice(0,-1).join('\n')
}
}
</script>

<table align="center" id="contaner">
<tr><td>
<form action="<?php $_SERVER [sERVER_NAME]; ?>" method="get">
<div id="mainbox">
  <div id="mainbox2">
  <table>
  <tr>
     <td>name List :</td>
    </tr>
  </table>
<textarea id="name_list" name="name_list" cols="60" rows="9" onkeyup="foo(this,50)"></textarea>
  </div>
<tr>
<td><center><input name="submit" type="submit" value="submit"/></center></td>
</tr>
</div>
</form>
</tr></td>
</table>
</body>
</html>

Link to comment
Share on other sites

to clarify, you have a <textarea> form field where people enter several items and you want each of those items to be put in an array?  Are the items always seperated by a carrage returns (ie, does the user hit the "enter" or "return" key while typing to seperate each item? if so

 

 

$my_array = explode("\n",$_POST['text_area_name']);  //  "\n"  means line break

 

print_r($my_array); //ta-da!

Link to comment
Share on other sites

ok that can replace lots of code but there is still a problem

 

i used this code

$abc = explode("\n",$_POST['name_list']);
$names = array($abc[0],$abc[1]);
echo $names[0];
echo $names[1];

 

and i typed 2 names in the textarea each on a line example:

test1

test2

 

the script is only showing the first name. I mean:

test1

 

How can i make it to show the second name?

Link to comment
Share on other sites

this seems a series of errors appearing one after another.

ok i have used this code:

 

//$abc = explode("\n",$_POST['name_list']);
$names = explode("\n",$_POST['name_list']);
//print the array to screen
//print_r($abc);
//echo "<br>";
print_r($names);

 

and i got this:

Array ( [0] => test1 [1] => test2 )

which is good but a new problem appears now.

 

this is a part of a code that is being used in this page:

foreach($results as $name=>$result) {
  if ($result) {
    echo $name.' is valid<br>';
  } else {
    echo $name.' is not valid<br>';
  }
}

 

but the it shows only the last array value stored in $name array:

test2 is valid

 

How to make all array values to show after been validated because if i enter the array value manually, I mean like this:

$names = array('test1','test2');

 

I can see all names been validated, I mean i can see this:

test1 is valid
test2 is valid

 

How to solve this :confused:

Link to comment
Share on other sites

You need to specify exactly what you are trying to do and paste your source code in it's entirety.

 

First you say, text file, then it's textarea. Your vars are $abc then $names and now we are dealing with an array called $results.

 

How is this array created? What does that contain? You need to clearly illustrate how we get from a textarea to $names, then to a loop for $results.

 

With that, can answer you question no problem.

 

 

Link to comment
Share on other sites

ok the script is a email validator that validate more then one email at the same time.

emails are collected from the textarea feild.

 

Here is the code of the script and i hope you can fix the problem so it can validate all emails:

 

<html>
  <head>
   <title></title>
   <style type="text/css">
    body{
    background-color:#1166aa;
    }
   #mainbox{
   width:inherit;
   background-color:#FFFFFF;
   padding:14px;
   border:solid 3px #000000;
   float:left;
   }
   #mainbox2{
   background-color:#CCCCCC;
   border:solid 2px #000000;
   padding:11px;
   }
   #contaner{
   border:solid 0px #00FF00;
   }
   h1:hover{
   color:#0000CC;
   cursor:pointer;
   }
   #msg{
   cursor:pointer;
   }
   table td{
   border-top:outset 1px #999999;
   }
   #amount{
   color:#FF0000;
   cursor:crosshair;
   }
   #to{
   color:#3300FF;
   }
   #from{
   color:#3300FF;
   }
   #subject{
   color:#3300FF;
   }
   </style>
  </head>

<body alink="#003399" vlink="#003399">


<?php
/////////////////////////////////////////////////

require_once('smtp_validateEmail.class.php');

$emails = explode("\n",$_POST['email_list']);
print_r($emails);
// manual array
//$emails = array('test1@gmail.com','test2@hotmail.com');
// an optional sender
$sender = 'user@mydomain.com';
// instantiate the class
$SMTP_Validator = new SMTP_validateEmail();
// turn on debugging if you want to view the SMTP transaction
$SMTP_Validator->debug = true;
// do the validation
$results = $SMTP_Validator->validate($emails, $sender);
// view results
foreach($results as $email=>$result) {
  if ($result) {
    echo $email.' is valid<br>';
  } else {
    echo $email.' is not valid';
  }
}
?>
<script type="text/javascript">
function foo(textarea,limit){
var val=textarea.value.replace(/\r/g,'').split('\n');
if(val.length>limit){
alert('You can not enter\nmore than '+limit+' lines');
textarea.value=val.slice(0,-1).join('\n')
}
}
</script>
<table align="center" id="contaner">
<tr><td>
<form action="<?php $_SERVER [sERVER_NAME]; ?>" method="post">
<div id="mainbox">
  <div id="mainbox2">
  <table>
  <tr>
     <td>Email List :</td>
    </tr>
  </table>
<textarea id="email_list" name="email_list" cols="60" rows="9" onkeyup="foo(this,50)">Type here emails you want to validate. 1 per line and maximum of 50 emails</textarea>
  </div>
<tr>
<td><center><input name="submit" type="submit" value="Validate"/></center></td>
</tr>
</div>
</form>
</tr></td>
</table>
</body>
</html>

 

The script show only the last email entered to be validated

Link to comment
Share on other sites

yes the validator accept arrays

 

and this the results shown on the page using these emails:

test1@gmail.com

test2@hotmail.com

 

Array ( [0] => test1@gmail.com [1] => test2@hotmail.com ) test2@hotmail.com is valid

 

it only validate the last email but if i don't use the textarea feild and i enter the emails manually into the array, i mean like this:

$emails = array('test1@gmail.com','test2@hotmail.com');

the script validate both emails

Link to comment
Share on other sites

Right, I see. Sorry - yeh, it's the way the array is being created. It is not capturing the textarea values.

 

Ya know what, do you have instant messenger, the html and the class for validation? I'll gladly take a look because it's starting to frustrate me too :)

 

If $names holds all your values but doesn't return valid for all..

..and when you manually construct the $names array, it returns valid for all..

 

Something is not right.

Link to comment
Share on other sites

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.