For a while now, I've been keeping track of phone number by enter them into a plain text file on my laptop and searching for numbers with a simple shell script named phone
cat /path/to/phonelist.txt | grep -i $1
This is fine and dandy if I'm actually using my laptop, but I needed a way to access the phone numbers from all of my computers. If you are playing Buzzword Bingo, put a checkmark on "cloud". There is a low-power computer in my home network that will perform the task of "cloud based phone number listing" wonderfully.
Here is the HTML of the interface
<form name='p' action='phone.php' method='post' target='result_frame'>
<input type="text" name="search_string">
<input type="submit" value="find">
<iframe name="result_frame" id="result_frame">
A simple form makes a request and the results are displayed in a targetted inline frame. Thus, there is not a full page refresh when the user selects a different name to search for. Oh shiney!
On to the searching code
//get the search string from the POST data
$s_str = $_POST['search_string'];
//escape the string
$esc_str = escapeshellcmd($s_str);
//where is the phone file?
$phone_file = "phonelist.txt";
$results = "";
//read the file into an array
$file_lines = file($phone_file);
//loop through the array of lines
foreach($file_lines as $line)
//is the search string in the line?
if( stristr($line,$s_str) )
//add the line to the results.
//are there results?
//if not, let the user know that there were no matches
$results = "no matches found for \"$s_str\"";
//create some basic HTML to display to the viewer
echo "<html><head><title>Results for $s_str</title></head>";
This is a very simple PHP script to search for matching lines in the text file.
A working example of this code is viewable at http://www.jezra.net/code_examples/phonenumbers
Things that could have been done different:
- the numbers could be put into a database
- a regular expression could search the text file
The key here was development time. Comparitive to the actual development time, creating a database, adding the numbers, and writing the code to retrieve the numbers would add a significant amount of time. Unfortunately, I didn't think of using a REGEX until after I had written the code, and since the code worked as expected, I felt no need to change it.
Now quit reading, and call a friend.