Forum Moderators: coopster

Message Too Old, No Replies

Need to remove empty part of arry after explode

         

Storyman

7:25 am on Oct 12, 2004 (gmt 0)

10+ Year Member



Text is pulled from a MySQL DB text field and then broken into an array using explode. The array is then displayed as a bulleted list.

A problem arises when there are extra returns at the end of the stored text, which means the bulleted list has extra bullets at the bottom of the list without any adjoining text.

Here is the code:
<?php
if (!empty($row_rsHomePage['mt_list'])) {
$thelist = nl2br($row_rsHomePage['mt_list']);
$newlist = explode('<br />', $thelist);
echo '<ul>';
foreach($newlist as $val) {
echo '<li>'.$val.'</li>';
}
echo '</ul>';
}
?>

As you probably already guessed I'm somewhat of a newbie at this.

Is there a PHP code that will remove the excess <br /> at the end of the file where there is not text? I've tried rtrim & trim without any change. It seems that it will work with whitespace, but not with '<br />'.

What would you suggest?

dreamcatcher

7:48 am on Oct 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using trim() before you use the nl2br function.

$thelist = trim(nl2br($row_rsHomePage['mt_list']));

mincklerstraat

7:55 am on Oct 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



dreamcatcher got this right I think but the code he probably means is:
$thelist = nl2br(trim($row_rsHomePage['mt_list']));

dreamcatcher

8:45 am on Oct 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks mincklerstraat. Posting before I`ve had my coffee is never a good thing. :)

Storyman

4:25 pm on Oct 12, 2004 (gmt 0)

10+ Year Member



mincklerstraat & dreamcatcher,

Thank you both for helping me along what seems like a steep learning curve.

The solution worked great.

dreamcatcher

5:34 pm on Oct 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad we could help you Storyman. :)