Page 1 of 1

Tab-Delimited Files

Posted: Thu Jun 30, 2005 9:04 pm
by pianoman
Hello-

I was wondering if you would be willing to help me, I can't imagine it is difficult. I have a text file that has mac addresses (tab) file-sharing-name (new line). What I need is for that file to be opened in php and for the mac addresses to be searchable.

My current (unworking) code is as follows:

Code: Select all

<?
 
$namelist = "namelist";
$nl = fopen($namelist, "r");
 
while($array = fgetcsv($nl,5000,"\t")) {
$key = array_search('00:11:22:33:44:55', $array);
echo($key);
}
 
fclose($nl);
 
?>
edit: Here is a clip of the text file...

Code: Select all

00:0x:9x:7x:7x:ex	Building-Room-Machine1
00:0x:9x:8x:4x:dx	Building-Room-Machine2
00:0x:9x:7x:1x:dx	Building-Room-Machine3
00:0x:9x:7x:0x:3x	Building-Room-Machine4
00:0x:9x:8x:cx:8x	Building-Room-Machine5
00:0x:9x:7x:fx:0x	Building-Room-Machine6
The eventual goal is to have this: a form asking only for the mac address, it will do a search, if it's not found, it will add it to a new line along with a name (on an additional form). If it is found, the same additional form will allow for the user to type in a new name and the line will be deleted and re-entered with the updated information. Sorry if that's not clear.

I would greatly appreciate any help you can offer me. Thank you for your time!

-pianoman

Posted: Fri Jul 01, 2005 12:07 am
by nigma
What about using [url=http://php.net/explode]explode()[/utl] to load the entries and their parts into an array?

Example pseudo-code:

Code: Select all

$file    = file_get_contents(FILE);
$entries = explode(explode($file,"\n"), "\t"); // Even indices refer to MAC part of each entry whereas odd indices refer to the other part

Posted: Sun Jul 03, 2005 2:33 pm
by pianoman
Hey-

Thanks for your reply...two questions this time...

when I added echo($entries[0]); to test this, nothing appeared... What did I do wrong?

Also, once that is working, how will I be able to search the array for mac addresses?

Thanks again!

Posted: Sun Jul 03, 2005 3:36 pm
by timvw

Code: Select all

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
  $parts = explode($line, "\t");
  echo "mac: $parts[0] name: $parts[1]<br>";
}
fclose($fp);
?>
rts = explode($line, "\t");
echo "mac: $parts[0] name: $partsї1]&lt;br&gt;&quote;;
}
fclose($fp);
?&gt;
ni_set('display_errors', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
echo "mac: $parts[0] name: $parts[1]<br>";
}
fclose($fp);
?>
n($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, &quote;\t&amALL);
ini_set('display_errors', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
echo "mac: $parts[0] name: $parts[1]<br&gt;&quote;;
}
fclose($fp);
?&gt;

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
echo "mac: $parts[0] name: $parts[1]<br>";
}
fclose($fp);
?>

ini_set('display_errors', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
ecet('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
echo &quote;mac: $partsї0] name: $partsї1]&lt;br&gt;&quote;;
}
fclose($fp);
?&gt;

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
echo &quote;mac: $partsї0] name: $partsї1]&lt;br&gt;&quote;;
}
fclose($fp);
?&gt;
[/php:1:05
echo "mac: $parts[0] name: $parts[1]<br>";
}
fclose($fp);
?>
'error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
echo &quote;mac: $partsї0] name: $partsї1]&lt;br>";
}
fclose($fp);
?>
s', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, &quote;\t&quote;);
echo &quote;mac: $partsї0] name: $partsї1]&lt;br&gt');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
echo "mac: $parts[0] name: $parts[1]<br>";
}
fclose($fp);
?>
t('display_errors', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, &quote;\t&quote;);
echo &quote;mac: $partsї0] name: $partsї1]&lt;br&gt;&quote;;
}
fclose($fp);
?&gt;

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, &quote;\t&quote;);
echo &quote;mac: $partsї0TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, &quote;\t");
echo &am
<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
echo "mac: $parts[0] name: $parts[1]<br>";
}
fclose($fp);
?>
melist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, &quote;\t");
echo "mac: $parts[0] name: $partsї1]&lt;br&gt;&quote;;
}
fclose($fp);
?&gt;
L);
ini_set('display_errors', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
echo "mac: $parts[0] name: $parts[1]<br>";
}
fclose($fp);
?&gt;
ne = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
echo "mac: $parts[0] name: ;
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
echo "mac: $parts[0] name: $parts[1]<br>";
}
fclose($fp);
?>
rs', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
echo "mac: $parts[0] name: $parts[1]<br>";
}
fclose($fp);
?>

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'namelist';

$fp = fopen($file, 'r');
while (($line = fread($fp, 2048)) !== FALSE)
{
$parts = explode($line, "\t");
echo "mac: $parts[0] name: $parts[1]<br>";
}
fclose($fp);
?>

Posted: Sun Jul 03, 2005 3:47 pm
by nigma
Because explode returns an array you can't in turn use it as an argument of another call to explode. I was just trying to illustrate the concept with the above snippet, hence the title pseudo-code.

Posted: Sun Jul 03, 2005 5:55 pm
by pianoman
Thank you all so much for your help, and for putting up with my n00b(esque) questions. If I had it my way this would be a simple MySQL application which I could write no problem, but It has to be a text file I guess. Thank you all for putting up with my questions. Hopefully there won't be many more on this project :D

Posted: Sun Jul 03, 2005 6:04 pm
by Chris Corbyn
pianoman wrote:Thank you all so much for your help, and for putting up with my n00b(esque) questions. If I had it my way this would be a simple MySQL application which I could write no problem, but It has to be a text file I guess. Thank you all for putting up with my questions. Hopefully there won't be many more on this project :D
That's what this community is built on :) Ask what you need ;)

Posted: Sun Jul 03, 2005 6:12 pm
by pianoman
timvw--

There is one error in your code, you had the arguments on the explode() function reversed. Once I fixed that, I had erratic results... they included:
mac: 00:0a:95:73:7a:e6 name: High-mclab2-eMac1 00:0a:95:8a:4a:d4
mac: 162-eMac5 00:0a:95:a1:f3:8a name: High-Rm162-eMac6 00:0a:95:80:ce:14
Interestingly, the 162-eMac5 (shown as a mac address when it is a partial name) is approximately a quarter of the way from the top while the first mac address displayed is the first. Many are skipped. So it shows a bunch of that jibberish... and then error-time :P
Notice: Undefined index: 1 in /Library/WebServer/Documents/namelist/index.php on line 11
mac: name:
There are hundreds, if not thousands, of those notices. Any ideas?

Posted: Mon Jul 04, 2005 7:45 am
by timvw
Well, you got the main idea... Up to you to get it right :D
Anyway, i'm going to be nice today... Here is code that does work...

Code: Select all

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'maclist.txt';

$fp = fopen($file, 'r');

while (!feof($fp))
{
    $line = fgets($fp);
    if ($line)
    {
    	$parts = split("\t", $line);
    	echo "mac: $parts[0] name: $parts[1]<br>";
	}
}
fclose($fp);
?>
t('display_errors', TRUE);

$file = 'maclist.txt';

$fp = fopen($file, 'r');

while (!feof($fp))
{
$line = fgets($fp);
if ($line)
{
$parts = split(&quote;\t&quote;, $line);
echo &quote;mac: $partsї0] name: $partsї1]&lt;br&gt;&quote;;
}
}
fclose($fp);
?&gt;
going to be nice today... Here is code that does work...

Code: Select all

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'maclist.txt';

$fp = fopen($file, 'r');

while (!feof($fp))
{
    $line = fgets($fp);
    if ($line)
    {
    	$parts = split("\t", $line);
    	echo "mac: $parts[0] name: $parts[1]<br>";
	}
}
fclose($fp);
?>
09afbd66eb]
<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'maclist.txt';

$fp = fopen($file, 'r');

while (!feof($fp))
{
$line = fgets($fp);
if ($line)
{
$parts = split(&quote;\t&quote;, $linway, i'm going to be nice today... Here is code that does work...

Code: Select all

&lt;?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'maclist.txt';

$fp = fopen($file, 'r');

while (!feof($fp))
{
    $line = fgets($fp);
    if ($line)
    {
    	$parts = split(&quote;\t&quote;, $line);
    	echo &quote;mac: $parts[0] name: $parts[1]<br>";main idea... Up to you to get it right 
Anyway, i'm going to be nice today... Here is code that does work...

Code: Select all

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'maclist.txt';

$fp = fopen($file, 'r');

while (!feof($fp))
{
    $line = fgets($fp);
    if ($line)
    {
    	$parts = split("\t", $line);
    	echo "mac: $parts[0] name: $parts[1]<br>";
	}
}
fclose($fp);
?>
i'm going to be nice today... Here is code that does work...

Code: Select all

&lt;?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'maclist.txt';

$fp = fopen($file, 'r');

while (!feof($fp))
{
    $line = fgets($fp);
    if ($line to get it right 
Anyway, i'm going to be nice today... Here is code that does work...

Code: Select all

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'maclist.txt';

$fp = fopen($file, 'r');

while (!feof($fp))
{
    $line = fgets($fp);
    if ($line)
    {
    	$parts = split(&quote;\t&quote;, $line);
    	echo &quote;mac: $parts&#1111;0] name: $parts&#1111;1]&lt;br&gt;&quote;;
	}
}
fclose($fp);
?&gt;
main idea... Up to you to get it right :D
Anyway, i'm going to be nice today... Here is code that does work...

Code: Select all

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'maclist.txt';

$fp = fopen($file, 'r');

while (!feof($fp))
{
    $line = fgets($fp);
    if ($line)
    {
    	$parts = split(&quote;\t&quote;, $line);
    	echo &quote;mac: $parts&#1111;0] name: $parts&#1111;1]&lt;br&gt;&quote;;
	}
}
fclose($fp);
?&gt;
[/php:1:09xt';

$fp = fopen($file, 'r');

while (!feof($fp))
{
    $line = fgets($fp);
    if ($line)
    {
    	$parts = split("\t", $line);
    	echo "mac: $parts[0] name: $parts&#111 get it right 
Anyway, i'm going to be nice today... Here is code that does work...

Code: Select all

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'maclist.txt';

$fp = fopen($file, 'r');

while (!feof($fp))
{
    $line = fgets($fp);
    if ($line)
    {
    	$parts = split("\t", $line);
    	echo "mac: $parts[0] name: $parts[1]<br>";
	}
}
fclose($fp);
?>
ay... Here is code that does work...

Code: Select all

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'maclist.txt';

$fp = fopen($file, 'r');

while (!feof($fp))
{
    $line = fgets($fp);
    if ($line)
    {
    	$parts = split("\t", $line);
    	echo "mac: $parts[0] name: $parts[1]<br>&am
<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$file = 'maclist.txt';

$fp = fopen($file, 'r');

while (!feof($fp))
{
    $line = fgets($fp);
    if ($line)
    {
    	$parts = split("\t", $line);
    	echo "mac: $parts[0] name: $parts[1]<br>";
	}
}
fclose($fp);
?>

Posted: Mon Jul 04, 2005 1:22 pm
by pianoman
Thanks for that.

I'm willing to do a lot of the work, but as this is my first go at file manipulation, I'm going to need lots of guidance :P . So here comes the next hurdle... I need to be able to have a variable, say $searchedmacaddress for instance (obviously being passed from a form in which the user punches in a mac address). So, somehow, I need to be able to search all the Mac Addresses within the file. If the mac address is in the file, it will return the current File Sharing name and allow input for a new one. When submitted, the old file-sharing name must be replaced with the new one, and the file modified to indicate such. If the mac address isn't found, it must ask for a new file sharing name, and should be added to the end of the file.

The second scenario (being not found) should be relatively easy to do, I think I've seen a tutorial for a similar situation. it would be something like

Code: Select all

$addition = $searchedmacaddress."\t".$newname;
fputs($fp, $addition);
I feel like I'm asking too much of you guys, but could anyone lend me a hand with the searching and replacing of the single line?ariable, say $searchedmacaddress for instance (obviously being passed from a form in which the user punches in a mac address). So, somehow, I need to be able to search all the Mac Addresses within the file. If the mac address is in the file, it will return the current File Sharing name and allow input for a new one. When submitted, the old file-sharing name must be replaced with the new one, and the file modified to indicate such. If the mac address isn't found, it must ask for a new file sharing name, and should be added to the end of the file.

The second scenario (being not found) should be relatively easy to do, I think I've seen a tutorial for a similar situation. it would be something like

Code: Select all

$addition = $searchedmacaddress."\t".$newname;
fputs($fp, $addition);
I feel like I'm asking too much of you guys, but could anyone lend me a hand with the searching and replacing of the single line? name, and should be added to the end of the file.

The second scenario (being not found) should be relatively easy to do, I think I've seen a tutorial for a similar situation. it would be something like

Code: Select all

$addition = $searchedmacaddress."\t".$newname;
fputs($fp, $addition);
I feel like I'm asking too much of you guys, but could anyone lend me a hand with the searching and replacing of the single line? my first go at file manipulation, I'm going to need lots of guidance :P . So here comes the next hurdle... I need to be able to have a variable, say $searchedmacaddress for instance (obviously being passed from a form in which the user punches in a mac address). So, somehow, I need to be able to search all the Mac Addresses within the file. If the mac address is in the file, it will return the current File Sharing name and allow input for a new one. When submitted, the old file-sharing name must be replaced with the new one, and the file modified to indicate such. If the mac address isn't found, it must ask for a new file sharing name, and should be added to the end of the file.

The second scenario (being not found) should be relatively easy to do, I think I've seen a tutorial for a similar situation. it would be something like

Code: Select all

$addition = $searchedmacaddress."\t".$newname;
fputs($fp, $addition);
I feel like I'm asking too much of you guys, but could anyone lend me a hand with the searching and replacing of the single line?$addition = $searchedmacaddress."\t".$newname;
fputs($fp, $addition);

I feel like I'm asking too much of you guys, but could anyone lend me a hand with the searching and replacing of the single line?

Posted: Mon Jul 04, 2005 1:43 pm
by timvw
I can only advise you to use a database instead of a file... Because you are going to run into a lot of problems...

Btw, you can only overwrite a part in a file if it's exactly the same length..

Posted: Mon Jul 04, 2005 10:27 pm
by pianoman
That's the problem though, this needs to be a file because what happens is all the machines on the network, upon bootup, poll this file and use it to get the proper file sharing name. Is there a way to modify just the part of the array I need after searching, and then writing the entire array to the file using some sort of loop?

Posted: Tue Jul 05, 2005 1:52 am
by timvw
Here it is in pseudo code:

open original file
open new file
read lines from original file
if line != modified
write line to new file
if line == modified
write modified to new file
close new file
close original file


Or use a database, and every time you insert/update/delete something, select all the rows and write them to file...