PHP class: Text to anything June 28th, 2007

 

I recently had a client who sent me a 300+ list of items they wanted placing into a drop down select menu. Beside the obvious accessibility issues they were quite set in their ways about what they wanted—per usual.

1. Adur District Council
2. Allerdale Borough Council
3. Alnwick District Council
4. Amber Valley Borough Council
5. Arun District Council
6. Ashford Borough Council
7. Aylesbury Vale District Council
8. Babergh district council
9. Barnsley Metropolitan Borough Council
10. Barrow-In-Furness Borough Council
...

Instead of adding each select option manually which would take approximately 16 years, I decided to whip up a quick script to do it for me automatically. Luckily they were all in a uniform fashion, so I removed the numbers from the start then just exploded it all into a nice array which I could do anything with. Here is the, modified for readability, script I came up with.

<?php
class TextToAnything
{
    /**
     * The original string
     * @access private
     * @var string
     */
    private $original;
 
    /**
     * The end result from parsing
     * @access private
     * @var array
     */
    private $complete = array();
 
    /**
     * The class constructor
     * @access public
     * @param string $list
     */
    public function __construct($list)
    {
        $this -> original = $list;
        $this -> removeNumbers();
        $this -> setToArray();
    }
 
    /**
     * Remove the list numbers from the string
     * @access private
     */
    private function removeNumbers()
    {
        $this -> original = preg_replace('#(\d+?.\s+?)#', '', $this -> original);
    }
 
    /**
     * Explode the list to an array
     * @access private
     */
    private function setToArray()
    {
        $this -> complete[] = $list = explode("\n", $this -> original);
    }
 
    /**
     * Output the final result
     * @access public
     * @return string
     */
    public function output()
    {
        $output = '';
 
        foreach ($this -> complete[0] as $item)
        {
            $output .= "<option value=\"{$item}\">{$item}</option>\n";
        }
        return $output;
    }
}
 
$councilList = new TextToAnything('1. Adur District Council
2. Allerdale Borough Council
3. Alnwick District Council
4. Amber Valley Borough Council
5. Arun District Council
6. Ashford Borough Council
7. Aylesbury Vale District Council
8. Babergh district council
9. Barnsley Metropolitan Borough Council
10. Barrow-In-Furness Borough Council');
 
echo "<select name=\"local_councils\">\n{$councilList -> output()}</select>";

Of course, the output() function could be made to do just about anything you wanted. Hopefully this will save some poor fellow the grief of having to do it all manually.

 

Comments so far

Adam Buxton
July 2009

nice code, although why not just drop the list into a text file traverse the file to create the selection? http://uk2.php.net/file you can explode each line by space and then ignore the numbers, which could be useful later? drops the code down to about ten lines?

 $line) {
    echo "$line\n";
}
//close the selection

not a criticism just wondered why you used such an expanded method to do the job?

Christopher Hill
July 2009

Adam, your method is going over HTTP causing more overhead, and you then also have the problem of running over each line calling functions multiple times.

Besides, this is a generic class which is meant to provide the basics for people to expand upon.

Ahmed
July 2009

i agree with Christopher. even if you think you may need to the numbers sometime later in the script all you would have to do is not call the removeNumbers function.