-
[PHP] file handling
i have something like this
Code:
1
1 23 0 10
end
2
20 0 0 0
end
in a file and i want to do a script that is able to read each number and if find an index(1 or 2) to return something that i'll use like this
PHP Code:
if(xx($str) == "index")
{
$a = first number;
$b = second number;
$c = ...;
$d = ...;
}
i have tried to do this for 2 days and i don't successful.
I can't make the script to read each number after it get the index.
The actual script send the fgets($f) to xx() and xx() check each character of the string.
Can somebody give me some hints?
-
Re: [PHP] file handling
Code:
$LineID = 0;
$Lines = "";
$Chars = 0;
$CharAt[$Chars][$LineID] = "";
$file = file("new.txt");
foreach ($file as $line)
{
$Lines[$LineID] = $line;
echo $Lines[$LineID];
echo "<b><br> Numeric characters found from above-mentioned string: <br></b>";
for($i = 0; $i < strlen($line); $i++)
{
if(is_numeric($line[$i]))
{
$CharAt[$Chars][$LineID] = $line[$i];
echo $CharAt[$Chars][$LineID];
$Chars++;
}
}
echo "<br>";
$LineID++;
}
echo "<br>";
echo "<b>Line 0; </b>". $Lines[0];
echo "<br>";
echo "<b>Character 0 found at string line 0; </b>". $CharAt[0][0];
you could start from that.
is_numeric for checksum.
2d arrays upon sepparating characters to their own group as some characters belong to a specific line.
in short;
Code:
$LineID = 0;
$Lines = "";
$Chars = 0;
$CharAt[$Chars][$LineID] = "";
$file = file("new.txt");
foreach ($file as $line)
{
$Lines[$LineID] = $line;
for($i = 0; $i < strlen($line); $i++)
{
if(is_numeric($line[$i]))
{
$CharAt[$Chars][$LineID] = $line[$i];
$Chars++;
}
}
$LineID++;
}
/* SAMPLE */
for($i = 0; $i < $LineID; $i++)
{
for($g = 0; $g < $Chars; $g++)
{
echo $CharAt[$g][$i];
}
}
-
Re: [PHP] file handling
The GIGO concept is great, this is a little more concise:
Code:
<?php
$data = file_get_contents('new.txt');
$lines = explode("\n",$data);
$mydata = array();
$idx = -1;
foreach($lines as $line){
$line = trim($line);
if($line == '')continue;
if($idx == -1){
if(is_numeric($line)&&is_int($line+0)){
// We expect this...
$idx = $line+0;
}else
continue;
}else{
// Split the numbers
$numbers = explode(' ',$line);
$mydata[$idx] = array();
foreach($numbers as $number){
$mydata[$idx][] = trim($number)+0;
}
$idx = -1;
}
}
// Display the data:
echo('My data:'."\n");
print_r($mydata);
?>
Or something like that.. I haven't tested it :x
-
Re: [PHP] file handling
thanks for reply,i successful xD