Extract Data From Text File

This is the right place to report general bugs, make suggestions or ask for help.
Post Reply
User avatar
WineMan
Posts: 106
Joined: Tue Sep 01, 2020 5:03 pm

Extract Data From Text File

Post by WineMan » Fri Jul 15, 2022 10:15 pm

This may not be the forum to use for this request and if it goes against any policies, just delete it.

I have set up a subscribe form on my blog which takes an email address and enters it into a text file on the server. The data is in this format:

me@myemail.com,you@youremail.com,we@youremail.com, etc

I want the data to be displayed in this format using php and it would be dynamic in that as email addresses are added, the column would display the new email addresses:

me@myemail.com
you@youremail.com
we@youremail.com

I have tried to use the explode() function but have not been able to get it to work. Right now, the code shown below pulls the data from the text file in the same format...that is in one continuous line. Can anyone of the gurus on this form help me with this?

Code: Select all

 <body><center>
        <table>
        <tr>
        <th>Current Subscribers</th>
        </tr>
<?php
if(!file_exists("data.txt"))
    {
        echo "The file cannot be found!";
        exit;
    }
$fp = fopen("data.txt", "r");
if(!$fp)
    {
        echo "File cannot be opened";
        exit;
    }
    echo "<table border = 2>";
    while(!feof($fp))
        {
           $info = fgets($fp);
            echo "<tr>";
            echo "<td>$info</td>\n";
            echo "<br />";
        }
        echo "</tr>\n";
        echo "</table>";
    fclose($fp)
 ?>
 </body>
Thanks

User avatar
WineMan
Posts: 106
Joined: Tue Sep 01, 2020 5:03 pm

Re: Extract Data From Text File

Post by WineMan » Sun Jul 17, 2022 5:43 pm

With the help of a developer friend, I was able to display the subscriber list in columnar format.

Here is the code in case anyone is interested.

Code: Select all

<body bgcolor="#fddabe">
<center>

<table bgcolor="#ffffff" width="15%" border=1 cellpadding=3 cellspacing=4>
  <tr><th>Subscriber List</th></tr>
<?php
if (file_exists("data.txt")) {
	$emlAddrString = file_get_contents("data.txt"); //string with comma-separated email addresses
	$emlAddrArray = explode(',', $emlAddrString); //array with email address
	foreach ($emlAddrArray as $emlAddress) {
	  echo "<tr><td>$emlAddress</td></tr>\n";
	}
} else {
  echo "<tr><td>The subscriber file cannot be found!</td></tr>\n";
}
?>
</table>
</body>
My friend is the developer of the LuxCal Web Calendar application. https://www.luxsoft.eu/

User avatar
Arvid
FlatPress Coder
Posts: 558
Joined: Sat Jan 26, 2019 7:40 pm
Contact:

Re: Extract Data From Text File

Post by Arvid » Thu Jul 28, 2022 1:45 pm

Sorry, was too slow for this one - explode() is of course the function of choice. Good to know it works now!

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests