Forum Moderators: coopster
this code gets all existing users:
function getAllUsers() {
$users = shell_exec('sudo /var/www/ftpadmin/./getUsers');
$output = shell_exec('cat users');
echo $output;
}
now echo $output gives something like:
user1
user2
user3
user4
user5
...
now I would like to echo this line by line like this:
<option>user1</option><option>user2</option><option>user3</option><option>user4</option><option>user5</option>
without line breaks!
but I'm stuck on how to do it.
grtz
When I say "a bit differently" I mean that your last twist made it just a little too complicated for str_replace() because you have to use each of the elements in more than one spot - in the value part and in the description part.
While testing this just now I noticed an oversight with both of my previous posts - if your string has a trailing \n, you'll get an empty <option></option>. You can fix that with trim()
$output = trim($output);
$ary = explode("\n",$output);
.
.
Wow, I decided to try a regular expression to see if I could do it, and I did - I'm astounded that it took me less than 4 hours. This one liner will do the same thing for you:
$output = preg_replace('/([a-zA-Z0-9]+)/','<option value="$1">$1</option>',$output);
It leaves the newlines in place, which is how I like my html source to look anyway.