acctman
acctman

Reputation: 4349

Adding space to each array output

I'm trying to add a space after each output but its only appearing on the last output

if ($en['mm_wmeet']) {
    $tmp = explode(",", $en['mm_wmeet']);
    for ($i = 0; $i < count($tmp); $i++) {
        $en['mm_wmeet'] = $tmp[$i]. "&nbsp;";
    }
}

Upvotes: 0

Views: 5298

Answers (3)

Alnitak
Alnitak

Reputation: 339816

The net effect of what you're doing is to just replace the commas with spaces, so you can use PHP's built-in str_replace function:

$en['mm_wmeet'] = str_replace(',', '&nbsp;', $en['mm_wmeet']);

If your search string was more complicated, you could use a regular expression instead.

For example if you wanted to also strip out any existing plain white space between the list items, you could use this:

$en['mm_wmeet'] = preg_replace('/,\s*/', '&nbsp;', $en['mm_wmeet']);

If you really want an additional trailing space after that, just append it.

Upvotes: 1

Robin Michael Poothurai
Robin Michael Poothurai

Reputation: 5664

best method is str_replace()

$en['mm_wmeet'] = str_replace(',', '&nbsp;', $en['mm_wmeet']);    

Upvotes: 4

vstm
vstm

Reputation: 12537

As Pekka said, use either .=:

$tmp = explode(",", $en['mm_wmeet']);
for ($i = 0; $i < count($tmp); $i++) {
    $en['mm_wmeet'] .= $tmp[$i] . "&nbsp;";
}

Or alternatively, use implode:

$tmp = explode(",", $en['mm_wmeet']);
$en['mm_wmeet'] = implode("&nbsp;", $tmp);

Upvotes: 6

Related Questions