User Tools

Site Tools


eg-259:lecture17

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
eg-259:lecture17 [2011/03/25 11:53] eechriseg-259:lecture17 [2013/03/06 19:22] – [Introduction to PHP (Part 2)] eechris
Line 4: Line 4:
 ====== Introduction to PHP (Part 2) ====== ====== Introduction to PHP (Part 2) ======
  
-**Lecture 17**: To be given on Tuesday 24th November2008.+**Contact Hour 22**: To be discussed on Wednesday 20th March2013.
  
 **Lecturer**: [[C.P.Jobling@Swansea.ac.uk|Dr Chris P. Jobling]]. **Lecturer**: [[C.P.Jobling@Swansea.ac.uk|Dr Chris P. Jobling]].
Line 280: Line 280:
    * //Code//:    * //Code//:
 <code php> <code php>
-<?php echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); ?>  +<<!DOCTYPE html>
-<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" +
-         "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">+
  
 <!-- sorting.php - An example to illustrate several of the <!-- sorting.php - An example to illustrate several of the
      sorting functions -->      sorting functions -->
-<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang="en"> +<html lang="en"> 
-  <head> <title> Sorting </title>+  <head>  
 +    <meta charset="utf-8" /> 
 +    <title> sorting.php - An example to illustrate several of the 
 +     sorting functions </title>
   </head>   </head>
   <body>   <body>
Line 430: Line 431:
   * //Code//:   * //Code//:
 <code php> <code php>
-<?php echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); ?>  +<!DOCTYPE html>
-<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1 //EN" +
- "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> +
 <!--    word_table.php <!--    word_table.php
  
-        Uses a function to split a given string of text into +Uses a function to split a given string of text into 
-        its constituent words. It also determines the frequency of +its constituent words. It also determines the frequency of 
-        occurrence of each word. The words are separated by +occurrence of each word. The words are separated by 
-        whitespace or punctuation, possibly followed by whitespace. +whitespace or punctuation, possibly followed by whitespace. 
-        The punctuation can be a period, a comma, a semicolon, a +The punctuation can be a period, a comma, a semicolon, a 
-        colon, an exclamation point, or a question mark. +colon, an exclamation point, or a question mark. 
-        The main driver program calls the function and displays +The main driver program calls the function and displays 
-        the results. +the results. 
-        --> +--> 
-<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang="en"> +<html lang="en"> 
-<head> <title> word_table.php </title> +  <head> 
-</head> +    <meta charset="utf-8" /> 
-<body> +    <title> Word Table </title> 
-<?php+  </head> 
 +  <body> 
 +    <?php
  
-// Function splitter +    // Function splitter 
-//  Parameter: a string of text containing words and punctuation +    //  Parameter: a string of text containing words and punctuation 
-//  Returns: an array in which the unique words of the string are +    //  Returns: an array in which the unique words of the string are 
-//           the keys and their frequencies are the values.+    //           the keys and their frequencies are the values.
  
-function splitter($str) {+    function splitter($str) {
  
-// Create the empty word frequency array+      // Create the empty word frequency array
  
-  $freq = array();+      $freq = array();
  
-// Split the parameter string into words+      // Split the parameter string into words
  
-  $words = preg_split("/[ .,;:!?]\s*/", $str);+      $words = preg_split("/[ .,;:!?]\s*/", $str);
  
-// Loop to count the words (either increment or initialize to 1)+      // Loop to count the words (either increment or initialize to 1)
  
-  foreach ($words as $word) { +      foreach ($words as $word) { 
-    $keys = array_keys($freq); +        $keys = array_keys($freq); 
-    if(in_array($word, $keys)) +        if (in_array($word, $keys)) 
-      $freq[$word]++; +          $freq[$word]++; 
-    else +        else 
-      $freq[$word] = 1; +          $freq[$word] = 1; 
-  }+      }
  
-  return $freq; +      return $freq; 
-} #** End of splitter+    }#** End of splitter
  
-// Main test driver+    // Main test driver
  
-  $str = "apples are good for you, or don't you like apples?+    $str = "apples are good for you, or don't you like apples?
 or maybe you like oranges better than apples"; or maybe you like oranges better than apples";
  
-// Call splitter+    // Call splitter
  
-  $tbl = splitter($str);+    $tbl = splitter($str);
  
-// Display the words and their frequencies+    // Display the words and their frequencies
  
-  print "<br /> Word Frequency <br /><br />"; +    print "<br /> Word Frequency <br /><br />"; 
-  $sorted_keys = array_keys($tbl); +    $sorted_keys = array_keys($tbl); 
-  sort($sorted_keys); +    sort($sorted_keys); 
-  foreach ($sorted_keys as $word) { +    foreach ($sorted_keys as $word) { 
-    print "$word $tbl[$word] <br />"; +      print "$word $tbl[$word] <br />"; 
-  +    
-?> +  ?> 
-</body> +  </body> 
-</html> +</html>
 </code> </code>
  
eg-259/lecture17.txt · Last modified: 2013/03/08 18:04 by eechris