line break for option in select box

I need to  populate a select box with the results of a query.  The values of the query are long so what i need to do is insert a line break so that the select box doesnt cause a horizontal wrap on the webpage.  this has been done before in CF by doing the following;

selecting a predefined point that would relate to how many characters could fit into the select box and not cause a horizontal wrap

if the particular value is longer than the predefined point (aka number of values) then continue;
starting at that point and going backwards, looking for " " in the string value

when the space (" ") is found, enter a line break.

what is the code to do this in php?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

$len = 8;  //Set your own length

//Cut the string into segments with $len length and put into an array
$temp = explode('\n',wordwrap($mystring, $len, "\n", 1));
//Get the first line and tack on "..."
$mystring = $temp[1] . "...";

check out wordwrap

and explode
tecumsehjoeAuthor Commented:
ok I'm slightly confused here, this is where I am;

<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<select name="MyNiceQuery">
$query="SELECT distinct my_name
            FROM my_table
            ORDER BY my_name";
      or die(mysql_error());
while ($rows=mysql_fetch_assoc($results)) {
      foreach($rows as $val1) {
            $temp = explode('\n',wordwrap($val1, 20, "\n", 1));
            $val2 = $temp."...";
            echo "<option>\n";
            echo $val2;
            echo "</option>\n";

I tried temp as an array and no luck, so i changed it, but recieved the same results, whats wrong here?
temp is an array of lines for example

if your string is "my really long string that I want to cut down a bit"
wordwrap will add \n so that the text wraps like so

my really long
string that I
want to cut
down a bit

now using explode we take each line and put it in an array so it would look like this
echo temp[1]; // my really long
echo temp[2]; //string that I
echo temp[3]; //want to cut
echo temp[4]; //down a bit

but you just want the first line so you need to just grab the first entry in the array:

in your code you put
>> $val2 = $temp."...";
which assigned an array $temp to $val2, but you want to assign the value of the first member of the array $temp to $val to (aka temp[1])
so your code should look like this:
$val2 = $temp[1]."...";

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

tecumsehjoeAuthor Commented:
thats how i 'tried temp as an array and no luck'

I get "Array" showing up in the select box several times
You cannot use a linebreak in an HTML <select>.  However it is possible to set the width of your <select> using CSS.
Try adding:
in your <select> tag, e.g.:
    <select name="myname" id="myname_id" style="width:50ex;">

Your box will be the given width and the options may be longer.

"ex" means the height of the character "x" which is approx. the width of a character. You can use "px" (=pixels) or "%" too.
Boris AranovichSenior Software EngineerCommented:
maybe modifying the content of the <option>s with a javascript, and breaking it with \n ?
tecumsehjoeAuthor Commented:
maybe splitting it into two different options, and when selecting either of the two ='s the same value?
i think in 'steve918' solution it not worked because  explode('\n',...   dont work. first argument should be string.

try this.

  echo "<select>\n";
  $val1 = "loooooo ooooooo ooooooooo oooooooo ooooooooo ooooooooo oooooooong";
  $temp = explode('$$',wordwrap($val1, 20, "$$", 1));
  foreach($temp as $key=>$val) {
    echo "<option value=1>\n";
    echo $val;
    echo "</option>\n";
  echo "</select>";

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.