tags:

views:

52

answers:

3

we unable to split the string following code.please Help us.

 <?php
$i=0;
$myFile = "testFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "no\t";
fwrite($fh, $stringData);
$stringData = "username \t";
fwrite($fh, $stringData);
$stringData ="password \t";
fwrite ($fh,$stringData);

$newline ="\r\n";
fwrite ($fh,$newline);
$stringData1 = "1\t";
fwrite($fh, $stringData1);
$stringData1 = "srinivas \t";
fwrite($fh, $stringData1);
$stringData1 ="malayappa \t";
fwrite ($fh,$stringData1);


fclose($fh);



?>
$fh = fopen("testFile.txt", "r");
$
while (!feof($fh)) {
$line = fgets($fh);
echo $line;
}

fclose($fh);
$Beatles = array('pmm','malayappa','sreenivas','PHP');

for($i=0;$i<count($Beatles);$i++)
{
if($i==2)
{

echo $Beatles[$i-1];
echo $Beatles[$i-2];

}
}
$pass_ar=array();
$fh = fopen("testFile.txt", "r");
while (!feof($fh)) {
$line = fgets($fh);
echo $line;
$t1=explode(" ",$line);

print_r($t1);
array_push($pass_ar,t1);
}

fclose($fh);

Thanks & Regards pmms

A: 

You're exploding on whitespace. Unless there is whitespace in the string your exploding then no, it won't work.

Try using the code markup to make your code a little more readable in order to get better quality responses from people.

Evernoob
A: 

If i read the code correctly you are writing the string seperated by \t but try to explode with spaces, use:

explode("\t", $string);
dbemerlin
+1  A: 

You could use fgetcsv, since you're just doing a standard tab-delimited input file. Given your sample file of:

no [tab] username [tab] password
1  [tab] srinivas [tab] malayappa

then

$lines = array();
$fh = fopen('testfile.txt', 'rb') or die ("can't open testfile.txt");
while($lines[] = fgetcsv($fh, 0, "\t") { // no line length limit, tab delimiter)
   ...
}

will give you

$lines = Array(
    0 => Array(
         0 => 'no ',
         1 => 'username ',
         2 => 'password '
    ),
    1 => Array(
         0 => 1,
         1 => 'srinivas ',
         2 => 'malayappa'
    )
);
Marc B