This is not ProjectBloodhound material, at least not first semester stuff. But if you find yourself running into highly structured data — such as the reports from a spreadsheet or a database application — you have the ability to easily manipulate that data in PHP.
This is a simple example, but you don’t have to limit yourself to doing simple things. Imagine a data structure like this:
Name[tab]Phone Number
Cathleen Collins[tab]602-369-9275
Greg Swann[tab]602-740-7531
In the file the code shown here as “[tab]” would be an actual tab character, and this kind of data goes by the arcane name of: A tab-delimited file.
Most programming languages were written by exacting people with abstract and elegant reasons for everything they did. PHP was written by overbooked programmers who needed to pound out new web pages as quickly as possible.
In consequence, PHP is optimized for dealing with highly structured data. Here is a short program that will take a tab-delimited phone number file as input and output reformatted phone numbers into the HTML stream. In other words, this code could produce a dynamically-updated phone list in what what might otherwise be a static web page:
<?PHP
auto_detect_line_endings;
$fi = fopen("PhoneNums.txt","r");
$line = fgets ($fi, 4096); // throw away fieldDef line
echo ("<b>Phone Numbers</b><br>");
while (!feof($fi))
{
$line = fgets ($fi, 4096);
list ($Name, $Phone_Number) = explode ("\t", $line);
if ($Name)
{
echo ("$Phone_Number <i>($Name)</i><br>");
}
}
fclose ($fi);
?>
There is one line that makes all the difference for this kind of work:
list ($Name, $Phone_Number) = explode ("\t", $line);
The stuff between the parenthesis are our known field names, and we’re using them as variable names for clarity’s sake. The explode function will create an array of separate fields from the text stored in the $line variable, splitting the fields on the tab character. The list function then inherits the array just created by explode and assigns each field to the appropriate field name variables. We only have two fields Read more



