3

Given, In a input form user can separate some specific names by new line and i am saving those names in an array. Then I print those names and at last say something like "thank you for the names".

         $var = "name1 



name2


        ";
    $your_array = explode("\n", $var);


        for($i=0; $i<(sizeof($your_array));$i++) {
          echo ($your_array[$i]);  
        }
echo "Thank you for the names"

But the problem is if someone enter more than one newline before or after a name then the next name show after some distance like below

    name1



    name2


Thank you for the names

How can escape this and output as below

name1
name2
Thank you for the names

I tried to use array_filter() but it don't work here.

Update:

If someone input
$var = "name1\nname2\n\n\n


           name3                     


name4";

output should be like

name1
name2
               name3
name4

But all the answers show like

name1
name2
name3
name4
3
  • just check if user give a newline in his input. If so just do not store it into the Array! Alternately, before printing you can check the same... Commented Dec 26, 2012 at 17:58
  • give like $your_array = array_filter($your_array); Commented Dec 26, 2012 at 18:00
  • User have to give newline to separate names. But if user give more than one new line after each name or give a new line then some space that causes the problem. I can not figure it out how to solve this. Commented Dec 26, 2012 at 18:01

5 Answers 5

2

Let's use a foreach loop for the sake of simplicity...

1) We need to iterate through the new array and trim the names so we lose all whitespace.

2) We echo out the name, if and only if the length of the name is 1 or more.

3) We add on a <br> tag to make each name appear on a new line.

<?php

$var = "name1\nname2\n\n\n";

$your_array = explode("\n", $var);

foreach($your_array as $name)
{
    if(preg_match("/[a-zA-Z0-9]{1,}/i", $name)) {
        $name = trim($name);
        echo (strlen($name) > 1) ? sprintf('%s<br>', $name) : '';
    }
}

echo "Thank you for the names";
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. But i still got a small problem. See my updated question.
@AshiqurRahman Remove the trim() function if you want to keep whitespace.
In that case, if someone give only space in a line then it will also show but I have to check that each array contains at least one character and i think i should check it with regx
@AshiqurRahman Okay, check out the regex added to ensure there is a name inside the line.
1
$your_array = explode(PHP_EOL, $var);

$names = array();

for ($i = 0; $i < count($your_array); $i ++ )
{
    // trim clean spaces and newlines surrounding each line
    // resulting value is assigned to $test
    // if this value is not empty the conditional is a true proposition
    // so is assigned to $names
    if ($test    = trim($your_array[$i]))
        $names[] = $test;
}

echo implode(PHP_EOL, $names) . PHP_EOL;
echo 'Thank you for the names.';

EDIT: using foreach

$your_array = explode(PHP_EOL, $var);

foreach ($your_array as $name)
{
    if ($test    = trim($name))
        $names[] = $test;
}

if ($names)
{
    // echo implode(PHP_EOL, $names) . PHP_EOL;
    echo implode('<br />', $names) . '<br />';
    echo 'Thank you for the names.';
}
else
    echo 'Names missing.';

EDIT 2

trim accept a charlist to explicit removes, so if you want maintain spaces, make a list with newlines and carriage return only.

trim($name, "\r\n");

NOTE: PHP_EOL is the correct new line char depending of server (unix or windows).

1 Comment

Thanks for the answer. But i still got a small problem. See my updated question.
1

Be a better PHP developer and use native PHP array functions:

$raw_arr = array('    trim  ', 'fine', '    ');

array_walk($raw_arr, function(&$value) {
  $value = trim($value);
});

$reduced_arr = array_filter($raw_arr);

print_r($reduced_arr);

Note: I have used a closure. If you are running PHP < 5.3, you'll need to move this to a UDF.

UPDATE

Seems like you don't wish to trim the user input. As such, you can drop array_walk and its closure.

$raw_arr = array('    trim  ', 'fine', '');
$reduced_arr = array_filter($raw_arr);

print_r($reduced_arr);

1 Comment

Thanks for the answer. But i still got a small problem. See my updated question.
0
for($i=0; $i<(sizeof($your_array));$i++) {
    if (empty($your_array[$i])) continue;
          echo ($your_array[$i]);  
        }
echo "Thank you for the names"

2 Comments

You don't account for trimming the spaces. You also have an unoptimized for loop.
Thanks for the answer. But i still got a small problem. See my updated question.
0

You can use array_filter + array_map + trim to discard empty space

$var = "name1 



name2


        ";

$array = array_filter(array_map("trim",explode("\n", $var)));
echo implode(PHP_EOL, $array) , PHP_EOL;
echo "Thank you for the names";

Or just use preg_match_all

preg_match_all("/\S?([a-z0-9]+)\S?/", $var,$array);
echo implode(PHP_EOL, $array[0]),PHP_EOL;
echo "Thank you for the names";

Both Would Output

name1
name2
Thank you for the names

Comments