Turn a text list into an HTML Select Option list with PHP

Easy solution to this basic HTML coding need posted over at Stack Overflow by a random clever bloke. Thanks RCB.

<?php

$fc = file_get_contents('./test.txt');
  $fc = utf8_encode ( $fc );
    $lines = explode("n", $fc);

$html = '<select>';
  foreach($lines as $line)
     $html .= '<option value="' . $line . '">' . $line . '</option>';

$html .= '</select>';
  echo $html;
?>

My only issue was with UTF-8 characters coming through garbled. Bodged a workaround using this encode entities plug-in for Dreamweaver.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *