I have opinions

PHP class: Text to anything

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.

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 .= "\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 "";

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.