This should work (added documentation to help):
Code: Select all
<?php
$short = 6; //wherever this comes from
//an array of all the full text (we'll leave the keys and deal with the offset later)
$full = array("Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten");
//if we don't have an integer, we'll have a problem...run a check
if (is_int($short)) {
//we've got an int, lets subtract 2 to count for the offset (Zero..one..two [we
started at two])
$text = $full[$short-2];
}
else {
//its not an integer, so convert it into one
$short = intval($short);
//as above
$text = $full[$short-2];
}
//output full text
echo $text;
?>
As for the other code you asked about, here it is with some comments added in:
Code: Select all
<?php
//basic function which reads the contents of the file into $lines as an array
$lines = file('test.txt');
//include this line if it helps to understand
//var_dump($lines);
//using a foreach (shouldn't...but it keeps it simple) to split the array into $line_num (the key) and $line (the content).
foreach ($lines as $line_num => $line) {
//make sure $line_num is an int, then increment it
$linenum = (int) $line_num;
$linenum++;
//quick check (as opposed to strstr()) which will return an int of the first position, or a boolean FALSE if not found
$t = strpos($line, "pizza");
//check to see whether or not it returned the boolean
if($t !== false)
{
//if not, it returned an int, therefore the text is found in the file on $linenum
echo "Pizza found in line #<b>{$linenum}</b> : " .$line. "<br />\n";
}
}
?>
Let me know if that doesn't clear things up.