Tag Archives: File IO

Web Design and Programming Pt 12

Web Design and ProgrammingIn this PHP video tutorial I’ll cover how to manipulate files with PHP. Specifically I cover the following:

  • Read file into an array
  • Read file into a string
  • Grab text off of web pages
  • Strip HTML tags
  • Write to files

Here is a link to the first article and video for this series, if you haven’t seen it Web Design and Programming.

All of the code follows the video. Use it in anyway. Leave any questions or comments below.

Code from the Video

<html>
<head>
<title><?php echo “Object Oriented Programming”;?></title>
</head>

<body>

<?php

# Creates a file descriptor and assigns it to $file
# The second attribute opens the file for read only purposes
# You can access any file online with this same function
# r+ – Read and Write from beginning of file
# w – Write only to beginning of file
# w+ – Delete contents and read and write to file
# a – Write only to the end of the file
# a+ – Read and Write to the end of the file
$file = fopen(“customers.txt”, “r”);

# While the end of file hasn’t been met print lines to screen
while (!feof($file))
{
# fgets() returns all the characters up to a newline or EOF
# You can also specify the number of characters to return in the 2nd attribute
echo fgets($file). “<br />”;
}

echo “<br />”;

# You can also read a file into an array
$customers = file(“customers.txt”);
foreach ($customers as $customer)
{
list($name,$street,$city,$state) = explode(“,”, $customer);
$state = trim($state);
echo “Name: ” . “$name <br />”;
echo “Street: ” . “$street <br />”;
echo “City: ” . “$city <br />”;
echo “State: ” . “$state <br /><br />”;
}

# Close the file
fclose($file);

$file4 = fopen(“customers.txt”, “a”);

$newCustomer = “\nRick,219 Almond St,Pittsburgh,PA”;
fwrite($file4, $newCustomer);

fclose($file4);

# You can also read a file into a string
$customers = file_get_contents(“customers.txt”);
$customers = explode(“\n”,$customers);

foreach ($customers as $customer)
{
list($name,$street,$city,$state) = explode(“,”, $customer);
$state = trim($state);
echo “Name: ” . “$name <br />”;
echo “Street: ” . “$street <br />”;
echo “City: ” . “$city <br />”;
echo “State: ” . “$state <br /><br />”;
}

$file2 = fopen(“http://www.wikileaks.ch/static/html/faq.html”, “rt”);

while (!feof($file2))
{
$faqs .= fgetss($file2, 1024);
}
fclose($file2);

$file3 = fopen(“justthefaqs.txt”, “wt”);
fwrite($file3, $faqs);

fclose($file3);

?>

</body>
</html>

Python 2.7 Tutorial Pt 16

Python 2.7 TutorialHere I continue to teach you how to use the Tkinter GUI module built into Python. I build an application that allows you to input files, edit them and then save them. I specifically show you how to make the following GUI widgets with Tkinter:

  • Menu Bars
  • Multi-Line Editable Text Boxes
  • Drop Down Menus

If you haven’t seen the previous Tkinter tutorial you should definitely check it out below first. Otherwise this won’t make any sense.

Continue reading Python 2.7 Tutorial Pt 16