0

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;";
    }
}
1
  • 2
    You are assigning, not adding. Use .= to add Commented Dec 24, 2011 at 9:51

3 Answers 3

6

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);
Sign up to request clarification or add additional context in comments.

Comments

4

best method is str_replace()

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.