Jump to content

Archived

This topic is now archived and is closed to further replies.

richinsc

PHP generated Text Files

Recommended Posts

Hello all,

Kind of a PHP Newbie. I have worked with PHP before but not to the extent of which I am trying to now. What I am trying to do is be able to add MAC addresses to a text file and then delete them from the text file when they are no longer needed. I have gotten the add part to work but am having trouble with the remove part. Below is the code that I have been trying to get to work. Please don't flame me if I seemed to not know what I am doing here. I am still kind of learning as I go. Bascially this code is to add and remove which MAC address from a text file.

[code]<html>
<head><TITLE>Mac Address Control</TITLE>
<form method="post" action="macdel.php">

<!-- DO NOT change ANY of the php sections -->

Mac Address: <br />
<input type="text" name="mac" size="17" />
<br />

<br />
Server:<br />
<select name="server" size="1">
<option value=" bwc-americanstores ">bwc-americanstores </option>
<option value=" bwc-bountiful ">bwc-bountiful </option>
<option value=" bwc-jordancommons ">bwc-jordancommons </option>
<option value=" bwc-sterling ">bwc-sterling </option>
<option value=" bwc-thad ">bwc-thad </option>
<option value=" bwc-utahcounty ">bwc-utahcounty </option>
</select>
<p>

<input type="submit" name="submit" value="Add" onClick="document.theForm.action='macadd.php'">
<input type="submit" name="submit" value="Remove" onClick="document.theForm.action='macdel.php'">
</p>
</form>
</head>
</html>[/code]

Above is the form that is used to add and del. The adding works and disreguard [code]<form method="post" action="macdel.php">[/code] as I am still working on the submit buttons. I am not concerned about them right now.

Here is the code for the macdel.php script below.

[code]<?php
$mac = $_POST['mac'];
$server = $_POST['server'];
?>
<html>
<body>
<!--DO NOT CHANGE ANY SECTIONS OF PHP CODE!!!-->
<?php

if ($server == " bwc-americanstores "){
    $filename = "bwc-americanstores"; // File which holds all data
    $inputstring = "$mac";
    
    $arrFp = file( $filename); // Opens the data file as an array
    $numLines = count( $arrFp ); // Count the elements in the array
    $arrWords = explode( ' ', $inputstring ); // Split the input string into words as an array
    $numWords = count ( $arrWords ); // Count the words in the string
    for($i=0; $i<$numWords; $i++) // Loop through the words in the string
    {
        for($j=0; $j<$numLines; $j++) // Loop through the lines in the text file
        {
                if(strstr($arrWords[$i], trim( $arrFp[$j] ))) // Search for restrive words
                    $arrWords[$i]= " "; // If it is replace will null value, trying to remove line
        }
        $outputString .= $arrWords[$i]. ' ';
    }
    echo $outputString; // Echo the string replacing the word
}
elseif ($server == " bwc-bountiful "){
    $filename = "bwc-bountiful"; // File which holds all data
    $inputstring = "$mac";
    
    $arrFp = file( $filename); // Opens the data file as an array
    $numLines = count( $arrFp ); // Count the elements in the array
    $arrWords = explode( ' ', $inputstring ); // Split the input string into words as an array
    $numWords = count ( $arrWords ); // Count the words in the string
    for($i=0; $i<$numWords; $i++) // Loop through the words in the string
    {
        for($j=0; $j<$numLines; $j++) // Loop through the lines in the text file
        {
                if(strstr($arrWords[$i], trim( $arrFp[$j] ))) // Search for restrive words
                    $arrWords[$i]= " "; // If it is replace will null value, trying to remove line
        }
        $outputString .= $arrWords[$i]. ' ';
    }
    echo $outputString; // Echo the string replacing the word
}
?>
<p align="center">
You have successfully removed <?php echo $mac ?> from  <?php echo $server ?>
<br>
</body>
</html>[/code]

All I want it to do is delete the mac that was entered in the box on the form page. After much searching on goolge to no avail for 4 hours, I figured I would post this here and see if someone could help while I [b]continue searching[/b] google. I also did a search on the forms of this site but could only find results for deleting from mySQL databases but nothing on text file. Please offer any suggestions. I am not looking for someone to do this for me. I am just looking for some help and maybe somone to help point me in the right direction. Once agian the [b]ADD[/b] fuctions works, need help with [b]REMOVE[/b]

Share this post


Link to post
Share on other sites
Hello again,

While searching google I did find the following resource and while it explains how to remove rows from text files and does give me a starting point I need to search to find the $mac in contents of text file and then delete it.

Here is the page that I found for working with text files and php. [a href=\"http://codewalkers.com/tutorials/57/1.html\" target=\"_blank\"]http://codewalkers.com/tutorials/57/1.html[/a]

Share this post


Link to post
Share on other sites
A Really quick way to remove a word or set of words instead of a string is to use the [b]str_replace()[/b] function, and replacing a value with "".

Just a future FYI :)

Share this post


Link to post
Share on other sites

×

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.