technology:
There is often a need to read lists of parameters from text files and this page tests one approach. There is a need to read structured fields from Drupal before installing the modules for structured fields. The same command string approach works in Drupal.
Start with a simple example input then we will add complications. The input could be in a file and there are lots of ways to read files. For this test page, we will start with data in a list implemented as a PHP array.
Line input with a fixed two column input
Each item is a command followed by data and the two parts separated by a vertical line, |, also called a pipe character because some operating systems assign a special use to the character in some circumstances.
$list = array('add|abc', 'add|xyz', 'delete|abc');The list is equivalent to a text file containing the following lines.
add|abc
add|xyz
delete|abc
How would we convert the lines to a table that is easy to process in PHP code? The following code reads the list of input and creates a new list with each part of each line as a separately named item. The explode() function is an easy way to split text based on a single separator. we could add trim() and other functions to remove spaces and other rubbish around the commands.
$commands_split = array();
foreach($list as $line)
{
$command_parts = explode('|', $line);
$commands_split[] = array('command' => $command_parts[0], 'data' => $command_parts[1]);
}
Lets print the data as a table.
| Command | Data |
|---|---|
| add | abc |
| add | xyz |
| delete | abc |
Line input with a fixed two column input and some comments
Add an option for comments. Some control files use ; to indicate comments and this test uses ;. We will allow for comments by themselves and at the end of a line. ; cannot be part of the data.
$list = array('; test file', 'add|abc', 'add|xyz ; comment at the end of the line.', 'delete|abc');The list is equivalent to a text file containing the following lines.
; test file
add|abc
add|xyz ; comment at the end of the line.
delete|abc
We will split the comment from the line before decoding the command. The result is in $line_parts and the command may be in $line_parts[0]. We trim $line_parts[0] to remove unwanted spaces then use $line_parts[0] only of it is not empty.
$commands_split = array();
foreach($list as $line)
{
$line_parts = explode(';', $line);
$line = trim($line_parts[0]);
if(!empty($line))
{
$command_parts = explode('|', $line);
$commands_split[] = array('command' => $command_parts[0], 'data' => $command_parts[1]);
}
}
Add an extra check for spaces
Your friends might add spaces anywhere when typing in the commands so add some extra checks for spaces. Add trim() as shown in the following line.
$commands_split[] = array('command' => trim($command_parts[0]), 'data' => trim($command_parts[1]));
Allow for an extra parameter
We will add a rename command that requires a second parameter then modify the code to fit the extra parameter. We will leave out comments and spaces for the remaining examples.
$list = array('add|abc', 'add|xyz', 'rename|xyz|x2', 'delete|abc');
The following example shows the code altered to count the split parts and create the output based on the number of parts.
$commands_split = array();
foreach($list as $line)
{
$command_parts = explode('|', $line, 3);
if(count($command_parts) == 3)
{
$commands_split[] = array('command' => $command_parts[0], 'data' => $command_parts[1], 'new name' => $command_parts[2]);
}
else
{
$commands_split[] = array('command' => $command_parts[0], 'data' => $command_parts[1]);
}
}
The following example shows another way to process the parts. This approach ensures there is always a third parameter in each row to make some types of subsequent processing easier. If you do not create a default value for the third parameter, you have to check if the parameter exists before using the parameter.
$commands_split = array();
foreach($list as $line)
{
$command_parts = explode('|', $line);
if(!isset($command_parts[2]))
{
$command_parts[2] = '';
}
$commands_split[] = array('command' => $command_parts[0], 'data' => $command_parts[1], 'new name' => $command_parts[2]);
}
Error checking and reporting
You could add checks for the number of parameters per type of command then highlight incorrect combinations. When processing string information of this type, you need to check a few examples then test for all the mistakes you find in the input. Do you reject all the input because one line is wrong or process the correct lines? The decision is yours based on your knowledge of the data and how it is used.
Add an optional sequence field
we want some commands to run first. We want some in a specific sequence. How can we add add an obvious sequence field without adding a field we might confuse with parameters? Add the sequence up front where we can distinguish the sequence from the alphabetic command string.
$list = array('add|abc', '1|add|xyz', '2|rename|xyz|x2', 'delete|abc');
How does the latest version of the data look in a table?
| Command | Data | Optional parameter | Sequence |
|---|---|---|---|
| add | abc | ||
| add | xyz | 1 | |
| rename | xyz | x2 | 2 |
| delete | abc |
How do we put it in sequence?
First split the table into sequenced and unsequenced commands. Note the sequenced commands have an additional automatic sequence, $sequenced['sequence'][], to allow for duplicate sequence numbers.
$sequenced = array();
$unsequenced = array();
foreach($commands_split as $command)
{
$sequence = $command['sequence'];
unset($command['sequence']);
if(empty($sequence))
{
$unsequenced[] = $command;
}
else
{
$sequenced[$sequence][] = $command;
}
}
We can now sort the sequenced commands by the sequence key using ksort().
ksort($sequenced);
Next we display the two lists in one table.
| Command | Data | Optional parameter | Sequence |
|---|---|---|---|
| add | xyz | 1 | |
| rename | xyz | x2 | 2 |
| add | abc | ||
| delete | abc |
Store it in Drupal 7
You can store the input data and the results in any version of the Drupal content management system. Drupal 7 provides some particularly easy ways to store and display multiple values. You would be wasting your time if you decided to use the web for storage or display then chose a content management system with less flexibility.
Where is this useful?
Simple text input formats let you send input through email and many other odd forms. You could send a list of commands or options to a server using email or type them in through a form. When you add Drupal to the mix, the data can be displayed on a page for visual verification before action. The list could be multiple entries in a field. The options are endless.
Some bug tracking systems, including Mantis, can read email to accept new posts. Those posts could contain commands for action if you added code in the right place, code similar to the code shown on this page.
Back in the early days of the Web, before the invention of SVG, I wrote a program to dynamically generate 2D images based on an assembly of parameters similar to what you would now perform using SVG. The parameters were formed into simple text commands so they could be transmitted through any format file, text, word processing, and edited using the most primitive editors plus all the editors that swamp the text in formatting. That old program had more code to extract the data from unwanted formatting and had some code to convert the parameters into graphical commands for use in the graphical software available back then. The results were spectacular and nearly bullet proof.
I found only one problem with that early program. In Germany, using a German format keyboard, I found one of the common English keyboard characters was hard to type and I made a slight change to the input format to remove that one difficult character. If you accept text from other countries, check their character sets and find out what is easiest on their keyboards.
Conclusion
Text input is a reliable alternative where the data is in a simple repetitive format. Using PHP, you can easily decode the input. Using Drupal, you can easily store the data from your PHP code. Using Drupal and PHP, you can easily access and use the data. This makes a Web based interface possible for a huge range of application settings, input data, and other items currently in files, emails, and all those lists you could scan in using OCR.








- Facebook Like
- Google Plus One
- Log in or register to post comments