Divide a textarea strings into groups depends on blank space

I have a textarea in my html form where user can enter numbers and after the form submit,php will echo sum of the numbers for example, If I enter

1
2

it will output,

sum is 3

My requirement is, if I put blank space(one or more), between each set of devices, it has to process each set of devices separately for example,

1
2

3
4

and above should display output as

sum is 3
sum is 7

But using below code I am getting output like

sum is 10

My Code:

<html>
<body>

<div class="cal-main">
<form  id="form1" name="form1" action=" " method="post" > 

<div class="input">Enter Numbers</div>
<div class="output"><span><textarea class="textarea" name="numbers" ></textarea></span></div>


<div class="submit1">
    <input  id="first_submit" type="submit"  name="first_submit" value="first_submit" />
</div>

</form>


<?php

if (isset($_POST['first_submit']))
{
?>
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value="">
<?php
$numbers = $_POST['numbers'];
$digits = explode(PHP_EOL.PHP_EOL, $numbers); // Favor PHP_EOL (end of line) to avoid cross OS issues
foreach($digits as $digit)
{

  $count = array_sum(explode(PHP_EOL, $digit));
  echo "sum is $count".PHP_EOL;
}
}
?>

</textarea>
</div>  
</body>
</html>

PHP FIDDLE SETUP

Answers


I would use preg_split and \r?\n:

$digits = preg_split('#(\r?\n){2,}#',$numbers); //2 or more \r\n (\r optional)
foreach($digits as $key => $digit)
{

    $count = array_sum(preg_split('#\r?\n#', $digit));
    echo "sum is $count".PHP_EOL;
    if($key < count($digits)-1) echo '<br />';
}

Output:

sum is 3
sum is 7

1  
2 

exactly equivalence with "1\n2" But

1
2

3
4

is same as "1\n2\n\n3\n4", you should split the string by "\n\n" to find the main groups then split each group by "\n" and get the sum of them, Sorry,i am not familiar whit PHP syntax, but it seems it's a easy work in php note that the "\n" is line feed with hex code = 10


$numbers = $_POST['numbers'];

$digits = explode("\n", $numbers); // explode by next line
$sums = array(); // array to put sums in
$i = 0;
foreach($digits as $digit) {
    $digit = trim($digit); // remove next line

    if(is_numeric($digit))
        $sums[$i] += $digit; // sum up
    else
        $i++; // next index (row wasn't number)
}

print_r($sums); 

// Output: Array ( [0] => 3 [1] => 7 )

Using preg_split is more appropriate, though new line could be different on different OS. My solution:

function split_new_lines($string){
    return preg_split('/\R/', $string);
}

$input = "
1
2

3
4

5
";

$lines = split_new_lines($input);

$sums = array();
$sum = null;
foreach($lines as $line){
    $line = trim($line);
    if(is_numeric($line))
    {
        if($sum === null)
            $sum = (int)$line;
        else
            $sum += (int)$line;
    }
    else
    {
        if($sum !== null)
            $sums[] = $sum;
        $sum = null;
    }
}
if($sum !== null)
    $sums[] = $sum;

notice that PHP_EOL is used to find the newline character in a cross-platform-compatible way. i have tested your code on in a windows/php 5.4.3 and it works as described above. when i run your fiddle, i get the error.


Need Your Help

Simple HTML/PHP - Module Position

php html css module alignment

On my website(http://rsoracle.com/index.php) the module on the right side(Most Voted Server) is too low. I've been getting at it for a while and cannot figure it out. I was wondering if any one had...

Storing test data in OpenCv4Android

java android c++ opencv

Is there any way to save test data in OpenCv4Android(in java not native) like FileStorage class in OpenCv in C++ ?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.