Forum Moderators: coopster

Message Too Old, No Replies

echo line by line

         

kristof_v

11:15 pm on Apr 14, 2007 (gmt 0)

10+ Year Member



hi all,

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

cameraman

2:04 am on Apr 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try
$output = '<option>' . str_replace("\n",'</option><option>',$output) . '</option>';
just above your echo line.

kristof_v

9:54 am on Apr 15, 2007 (gmt 0)

10+ Year Member



works great cameraman!
thx :)

kristof_v

2:36 pm on Apr 16, 2007 (gmt 0)

10+ Year Member



one more question,
do you know how to provide the username also in the <option> tag like this: <option value="user1">user1</option><option value="user2">user2</option>...?

Sagaris

3:48 pm on Apr 16, 2007 (gmt 0)

10+ Year Member



In that case replace the line provided by camerman with this.

$output = '<option value="'.$output.'">' . str_replace("\n",'</option><option>',$output) . '</option>';

HTH

cameraman

5:24 pm on Apr 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd do that a bit differently:
$ary = explode("\n",$output);
$output = '';
foreach($ary as $one)
$output .= "<option value=\"$one\">$one</option>";

I think a regular expression would make shorter work of it but I'm not good at regular expresions.

Sagaris

1:09 pm on Apr 17, 2007 (gmt 0)

10+ Year Member



Cameraman - I was just wondering why you would do it that way? Is there a particular performance benefit?

I ask earnestly as I'm trying to to up my PHP game to a level where I can code more efficiently so explanations from those with more experience as to their thinking is really useful!

Thanks

cameraman

6:59 pm on Apr 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The solution Sagaris posted produces this:
<option value="user1
user2
user3
user4
user5">user1</option><option>user2</option><option>user3</option><option>user4</option><option>user5</option>

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.

kristof_v

7:44 pm on Apr 17, 2007 (gmt 0)

10+ Year Member



thx guys!
works great now :)