Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP] file handling

Kingdom of Shadows
Loyal Member
Joined
Jul 13, 2007
Messages
923
Reaction score
320
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:
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?
 
Master Summoner
Joined
Jan 11, 2009
Messages
505
Reaction score
8
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];	
	}
}
 
Last edited:
Ginger by design.
Loyal Member
Joined
Feb 15, 2007
Messages
2,340
Reaction score
653
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
 
Last edited:
Back
Top