PHP Week 4

<?php
//opens file electricity.txt and arrays content
$file = "electricity.txt";
$fh = fopen($file, 'r');
$data = fread($fh, filesize($file));
$array = file($file);
fclose($fh);
echo "\n$file file contents:\n";
print_r($array);
?>

Fany-Savinas-Mac:PHP Fany$ php open_electricity.php
electricity.txt file contents:
Array
(
[0] => Here is a dissertation on physical science for your enlightenment.
[1] => I don't know where it came from so it must be true!
[2] =>
[3] => [ Update: This appears to have come from Dave Barry, and the same text
.........
[85] => operations to the human eyeball, provided they remember to change the
[86] => power setting from "Bulldozer" to "Eyeball."
)
-------

<?php
//opens file names.rtf and can tell how many times a specified name is written
$file = "names.rtf";
$fh = fopen($file, 'r');
$data = fread($fh, filesize($file));
echo "Enter name: ";
//trim removes white space at the end of string
$name = trim(fgets(STDIN));
echo "\nName repeated this many times:";
//counts in $data how many times $name appears
echo substr_count("$data", "$name");
echo "\n\n";
fclose($fh);
?>

Fany-Savinas-Mac:PHP Fany$ php count_repeats.php
Enter name: Keanu

Name repeated this many times:3

------

<?php
echo "Input word:";
$noun = trim(fgets(STDIN));
echo "Input name:";
$name = trim(fgets(STDIN));
echo "\n$noun\n";
//counts characters in string
echo strlen($noun);
echo "\n$name\n";
date_default_timezone_set('UTC');
//Monday 1st of January 2011 01:01:01
echo date('l jS \of F Y h:i:s');
echo "\n\n";
?>

Fany-Savinas-Mac:PHP Fany$ php noun_name_date.php
Input word:reciprocity
Input name:Fany

reciprocity
11
Fany
Tuesday 26th of April 2011 09:00:33

------

<?php
echo "Input word:";
$noun = trim(fgets(STDIN));
echo "Input name:";
$name = trim(fgets(STDIN));
$file = "noun_name_date.txt";
$handle = fopen($file, 'a');
date_default_timezone_set('UTC');
$date = date('l jS \of F Y h:i:s');
$data = "\n".$noun."\n".strlen($noun)."\n".$name."\n".$date."\n";
fwrite($handle, $data);
fclose($handle);
?>

Fany-Savinas-Mac:PHP Fany$ php noun_name_date_append.php
Input word:reciprocity
Input name:Fany

This entry was posted in Computing and tagged . Bookmark the permalink.

Leave a reply