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

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.

  1. <?php
  2. class TextToAnything
  3. {
  4.     /**
  5.      * The original string
  6.      * @access private
  7.      * @var string
  8.      */
  9.     private $original;
  10.  
  11.     /**
  12.      * The end result from parsing
  13.      * @access private
  14.      * @var array
  15.      */
  16.     private $complete = array();
  17.  
  18.     /**
  19.      * The class constructor
  20.      * @access public
  21.      * @param string $list
  22.      */
  23.     public function __construct($list)
  24.     {
  25.         $this -> original $list;
  26.         $this -> removeNumbers();
  27.         $this -> setToArray();
  28.     }
  29.  
  30.     /**
  31.      * Remove the list numbers from the string
  32.      * @access private
  33.      */
  34.     private function removeNumbers()
  35.     {
  36.         $this -> original preg_replace('#(\d+?.\s+?)#'''$this -> original);
  37.     }
  38.  
  39.     /**
  40.      * Explode the list to an array
  41.      * @access private
  42.      */
  43.     private function setToArray()
  44.     {
  45.         $this -> complete[] = $list explode("\n"$this -> original);
  46.     }
  47.  
  48.     /**
  49.      * Output the final result
  50.      * @access public
  51.      * @return string
  52.      */
  53.     public function output()
  54.     {
  55.         $output '';
  56.  
  57.         foreach ($this -> complete[0] as $item)
  58.         {
  59.             $output .= "<option value=\"{$item}\">{$item}</option>\n";
  60.         }
  61.         return $output;
  62.     }
  63. }
  64.  
  65. $councilList = new TextToAnything('1. Adur District Council
  66. 2. Allerdale Borough Council
  67. 3. Alnwick District Council
  68. 4. Amber Valley Borough Council
  69. 5. Arun District Council
  70. 6. Ashford Borough Council
  71. 7. Aylesbury Vale District Council
  72. 8. Babergh district council
  73. 9. Barnsley Metropolitan Borough Council
  74. 10. Barrow-In-Furness Borough Council');
  75.  
  76. 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.


Related articles

Top | Post a comment | Permalink

User submitted comments

1 adam buxton wrote

29th June, 2007


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?

  1. <?php
  2. // Get a file into an array.  In this example we'll go through HTTP to get
  3. // the HTML source of a URL.
  4. $lines file('http://www.example.com/');
  5. //start the selection by creating the code
  6. // Loop through our array, show HTML source as HTML source; and line numbers too.
  7. foreach ($lines as $line_num => $line) {
  8.     echo "<option>$line</option>\n";
  9. }
  10. //close the selection

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

2 adam buxton wrote

29th June, 2007


grr the above comment as stripped the options tag line in the post, hope you get the idea I’m asking about?

3 Christopher Hill wrote

29th June, 2007


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 exapand upon.

4 Ahmed wrote

5th July, 2007


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.


Penny for your thoughts

Comment posting guide

Your real name will be displayed as the author of the post. Real names are preferred, both first and second names, but nicknames/alter-egos are permitted. Any comments with the author name whose primary aim is to promote their website, and/or company will be removed.

If you supply a website URI then your real name will be clickable to that site. Only one sub-directory is permitted; e.g. http://www.example.org/directory.

Accepted comment input

  • <strong>…</strong>
  • <em>…</em>
  • <blockquote>…</blockquote>
  • <tt>…</tt>
  • <a href="…">…</a>
  • <code>…</code>




Recent articles

Other topics I ramble on about