Permalink
Browse files

Merge branch 'homework-solved-local' into homework-solved

  • Loading branch information...
2 parents 107f855 + 2587ad8 commit 23c171ebe16d7ad6a20cb60b95641d70e98dbef2 @shevron committed Apr 4, 2012
@@ -13,7 +13,7 @@
$possibleFiles = get_possible_files();
-if (isset($_GET['file']) && $_GET['file']) {
+if (isset($_GET['file']) && $_GET['file'] && in_array($_GET['file'], $possibleFiles)) {
$file = __DIR__ . '/../' . $_GET['file'];
$source = highlight_file($file, true);
} else {
View
@@ -36,3 +36,69 @@
// Correct answers
$answers = array('Arthur', 0, 'To seek the holly grail');
+
+$questionId = 0;
+
+if (isset($_GET['q']) && $_GET['q'] > 0 && $_GET['q'] <= count($questions)) {
+ $questionId = (int) $_GET['q'];
+}
+
+$result = null;
+if ($questionId == count($questions)) {
+ // We have answers to all questions
+ $result = true;
+ foreach($answers as $questId => $answer) {
+ if (! isset($_POST["question-$questId"])) {
+ $result = false;
+ break;
+ }
+
+ if ($_POST["question-$questId"] != $answer) {
+ $result = false;
+ break;
+ }
+ }
+}
+
+?>
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Bridge Of Death</title>
+</head>
+
+<body>
+ <h1>Welcome to the Bridge of Death</h1>
+<?php if ($result === null): ?>
+ <h2>Question <?php echo $questionId + 1 ?> out of <?php echo count($questions); ?></h2>
+ <form method="post" action="?q=<?php echo $questionId + 1; ?>">
+<?php foreach($questions as $qId => $question): ?>
+<?php if ($qId == $questionId): ?>
+ <label>Q: <?php echo $questions[$questionId]; ?></label><br />
+ A:
+<?php if (isset($choices[$questionId])): ?>
+ <select name="question-<?php echo $questionId ?>">
+<?php foreach($choices[$questionId] as $choiceId => $choice): ?>
+ <option value="<?php echo $choiceId ?>"><?php echo $choice ?></option>
+<?php endforeach; ?>
+ </select><br />
+<?php else: ?>
+ <input type="text" name="question-<?php echo $questionId ?>" />
+<?php endif; ?>
+<?php else: ?>
+ <input type="hidden" name="question-<?php echo $qId; ?>" value="<?php echo (isset($_POST["question-$qId"]) ? $_POST["question-$qId"] : '') ?>" />
+<?php endif; ?>
+<?php endforeach; ?>
+ <input type="submit" value="Next &raquo;" />
+ </form>
+<?php elseif ($result): ?>
+ <div class="result-correct">
+ <h2>Correct, off you go!</h2>
+ </div>
+<?php else: ?>
+ <div class="result-wrong">
+ <h2>Wrong! you don't cross!</h2>
+ </div>
+<?php endif; ?>
+</body>
+</html>
View
@@ -0,0 +1,21 @@
+<?php
+
+/**
+ * Reading data from a local file
+ */
+
+// Open file for reading
+$handle = fopen(__DIR__ . '/data/lines.txt', 'r');
+if (! $handle) {
+ echo "Unable to open file!\n";
+ exit(1);
+}
+
+echo "<ol>";
+while ($line = fgets($handle)) { // fgets - read until end of line
+ $line = trim($line);
+ echo "<li>" . htmlspecialchars($line) . "</li>";
+}
+echo "</ol>";
+
+fclose($handle);
View
@@ -0,0 +1,18 @@
+<?php
+
+// Opening a file in 'w' mode will delete any
+// existing content. Open in 'a' mode to append
+
+// Read all file into an array of lines
+$data = file(__DIR__ . '/data/lines.txt');
+
+$outFile = sys_get_temp_dir() . '/output.txt';
+$output = fopen($outFile, 'w');
+for ($i = count($data) - 1; $i >= 0; $i--) {
+ $line = strrev(trim($data[$i])) . "\n";
+ fwrite($output, $line);
+}
+
+// For readability purposes let's dump it to output
+header("Content-type: text/plain");
+readfile($outFile);
@@ -0,0 +1,11 @@
+<?php
+
+/**
+ * Check if a string contains a character
+ */
+
+$pattern = '/[^\w]Shahar\s/';
+
+if (preg_match($pattern, $_GET['name'])) {
+ echo "Your name is Shahar too?";
+}
@@ -0,0 +1,14 @@
+<?php
+
+/**
+ * Find all capitalized words in file
+ */
+
+$pattern = '/^(\w+)@(([a-z]+\.)*[a-z]+)$/';
+
+if (preg_match($pattern, $_GET['email'], $match)) {
+ echo "Got an email address! Domain is " .
+ $match[2] . ", user is " . $match[1];
+} else {
+ echo "Not an email address...";
+}
View
@@ -0,0 +1,15 @@
+<?php
+
+/**
+ * Linkify URLs
+ */
+
+$input = file_get_contents(__DIR__ . '/data/links.txt');
+
+// Linkify URLs
+$search = '#(https?://\S+)#';
+$htmlFile = preg_replace($search,
+ '<a href="\1">\1</a>',
+ $input);
+
+echo nl2br($htmlFile);
View
@@ -0,0 +1,12 @@
+In pursuit of knowledge,
+every day something is added.
+In the practice of the Tao,
+every day something is dropped.
+Less and less do you need to force things,
+until finally you arrive at non-action.
+When nothing is done,
+nothing is left undone.
+
+True mastery can be gained
+by letting things go their own way.
+It can't be gained by interfering.
View
@@ -0,0 +1,6 @@
+You can learn a lot more about PHP regular expressions
+at http://il2.php.net/pcre
+
+For example, we did not discuss pattern modifiers (http://il2.php.net/manual/en/reference.pcre.pattern.modifiers.php) at all.
+
+You can also learn about Regular Expressions in general at http://en.wikipedia.org/wiki/Regular_expression

0 comments on commit 23c171e

Please sign in to comment.