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

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



# 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

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

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


# 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);

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




4 Responses to “Web Design and Programming Pt 12”

  1. Lucas says:

    Hi Derek

    Thank you for the tutorial. Again it’s a piece of good work. One question here – my browser shows “Notice: Undefined variable: faqs in C:\xampp\htdocs\one.php on line…”
    The code does its job and populates the file with some info from the webpage but its very untidy.

    Is it possible to get some more neat text in the file which contains the info from the webpage?

    Thanks in advance for your answer

    • admin says:

      Thank you 🙂 If I ever get an error report there is always one consistency and that is xampp. It seems to be very buggy. If you want to better simulate how your code will run on a real server, get wubi. It is free and allows you to install Ubuntu on your machine like any other application.

      If you don’t like it you can just delete it. The great benefit is that most every tool you’ll ever need is already installed by default or easily installed with no hassle.

      I hope that helps

      • Lucas says:

        I will definitely check this software out, however there is a thing to explain. So you are recommending Linux basically, right? Is it enough to install Wubi to have my PHP code verified correctly? I mean Xampp has MYSQL, Apache, Tomcat and so on. Can you get it with Ubuntu 12.04 LTS? Or shall I install Ubuntu Server as well? I need those elements to learn SQL or to use PHP code with my webpages. They are an asset of some sort, I think.

        Please respond

        • admin says:

          Wubi is definitely enough to develop with. Everything is so easy to install that it will just make you smile. Apache, Tomcat, MySQL, etc. is easy to work with. I prefer it over xampp

Leave a Reply to admin Cancel reply

Your email address will not be published.