0

I would like to add a blank line at end of the last $member in below code to differentiate between each group. I have tried with below code

foreach($members as $key => $member)
{
    $member = trim(preg_replace('/\s\s+/', ' ', $member));
    $dev = " $member part of Form; <br>";
    echo str_replace("<br>", "\r\n", $dev);

    if($key == count($member)-0) 
    {
        $space = "<br>";
        echo str_replace("<br>", "\r\n", $space);
    }
}

But It adding a blank space after each 2 line. How can I add space only at end of the last string ?

soulmerge
  • 73,842
  • 19
  • 118
  • 155
acr
  • 1,674
  • 11
  • 45
  • 77

4 Answers4

2
foreach($members as $key => $member)
{

$member = trim(preg_replace('/\s\s+/', ' ', $member));
$dev = " $member part of Form; <br>";
echo str_replace("<br>", "\r\n", $dev);
if($key == count($members)-1) <!--- ** check this line ---->
{
$space = "<br>";
echo str_replace("<br>", "\r\n", $space);
}

}

** You have counted the value of the key ($member) but actually it will be count of the original array ($members). Here $key starts from zero and it will end previous number of total array count.

I don't know your array structure of $members. You may use bellow code

$count = count($members);
$i  = 1;
foreach($members as $key => $member)
{

$member = trim(preg_replace('/\s\s+/', ' ', $member));
$dev = " $member part of Form; <br>";
echo str_replace("<br>", "\r\n", $dev);
if($i == $count) 
{
$space = "<br>";
echo str_replace("<br>", "\r\n", $space);
}
$i ++;
}
monojit
  • 605
  • 5
  • 19
1

I'm not sure I understand what you're asking. If you want to add a newline after the last item, you can do so after the loop:

foreach ($members as $key => $member) {
    # ...
}
echo "\r\n";

If you need it inside the loop, you will need to test for the last key. One way of doing this is by manipulating the array's internal pointer:

# Set internal pointer of array to the last entry:
end($members)
# Get the key of that last entry:
$lastKey = key($members)
foreach ($members as $key => $member) {
    # ...
    if ($key === $lastKey) {
        echo "\r\n";
    }
    # ...
}
soulmerge
  • 73,842
  • 19
  • 118
  • 155
1

To add a newline "at the end of the last string", just echo it after the loop. But use the constant PHP_EOL instead of "\r\n".

Concatenating PHP_EOL to the end of each line will give you output that displays more or less the same in a browser and at the command line. Echoing one more PHP_EOL after the loop will insert a blank line that's visible from the command line, but not from the browser. If you need a blank line (more whitespace) in the browser, use CSS instead.

Since you seem to know how to replace <br> tags already, I ignored that part of your code. If you really need to replace those tags instead of just making your text more readable from the command line, you can easily do that.

$ cat code/php/test.php
<?php
$members = array(0 => 'Member0', 1 => 'Member1', 2 => 'Member2', 3 => 'Member3');
foreach($members as $key => $member)
{
    $member = trim(preg_replace('/\s\s+/', ' ', $member));
    echo " $member part of Form; <br>".PHP_EOL;
}
echo PHP_EOL;
?>
$ php code/php/test.php
 Member0 part of Form; <br>
 Member1 part of Form; <br>
 Member2 part of Form; <br>
 Member3 part of Form; <br>

$
Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
0
$numItems = count($members);
$i = 0;
foreach($members as $key=>$member) {
  if(++$i === $numItems) {
    $space = "<br">;
    echo str_replace("<br>", "\r\n", $space);
  }
}

Find the last element of an array while using a foreach loop in PHP

Community
  • 1
  • 1
Lkopo
  • 4,798
  • 8
  • 35
  • 60