0

In a textarea, I'm pasting random datas following one pattern:

Data1    Data2    Data3    Data4    Data5    Data6    Data7  
Data8    Data9    Data10    Data11    Data12    Data13    Data14

Like:

name [4*SPACE] surname [4*SPACE] address [4*SPACE] country [4*SPACE] phone [4*SPACE] sex [4*SPACE] age [4*SPACE] [line break]

And in this pattern, the last data (age here) can be empty.

So I did:

<form>
   <textarea rows="15" cols="100" name="query" id="pastedata">Paste datas there.</textarea>
   <p style="margin-left: 320px;"><input type="submit" name="submit"value="Submit" /></p>
</form>

<?php


if(isset($_GET['query'])) {
    $query = $_GET['query'];
$query = str_replace('    ', ',', $query);
$query = str_replace("\n", ',', $query);
$info = explode (',', $query);
$result = array_map('trim', $info);
print_r(array_chunk($result, 7));
}

It does its job... PHP returns a multidimensionnal array following the pattern of 7 columns, except if the last data is empty so it will shift the next array's first row.

How can I say to PHP that if in any array the last value is empty then just return something like 'Empty data' ?

This is my first PHP lines so I'm sure the structure is broken.


EDIT: Thanks a lot everyone, it now works as expected !

2
  • Can you give an example of the result you are seeking? Commented Oct 20, 2017 at 12:59
  • just apply trim on the input? $query = trim($_GET['query']) Commented Oct 20, 2017 at 13:01

4 Answers 4

1
<?php

$_GET['query'] = 'Data1    Data2    Data3    Data4    Data5    Data6    Data7 
Data8    Data9    Data10    Data11    Data12    Data13    Data14';


if(isset($_GET['query'])) {
   $query = $_GET['query'];
   $query = explode(PHP_EOL, $query);
   foreach($query as &$q){
       $q = trim($q);
       $q = explode('    ', $q);
   }
var_dump($query);
}

first explode string at EOL than for each line trim whitespaces and explode them on [4*space]

this will be the result:

array
  0 => 
    array
      0 => string 'Data1' (length=5)
      1 => string 'Data2' (length=5)
      2 => string 'Data3' (length=5)
      3 => string 'Data4' (length=5)
      4 => string 'Data5' (length=5)
      5 => string 'Data6' (length=5)
      6 => string 'Data7' (length=5)
  1 => &
    array
      0 => string 'Data8' (length=5)
      1 => string 'Data9' (length=5)
      2 => string 'Data10' (length=6)
      3 => string 'Data11' (length=6)
      4 => string 'Data12' (length=6)
      5 => string 'Data13' (length=6)
      6 => string 'Data14' (length=6)
Sign up to request clarification or add additional context in comments.

Comments

0

There are definitely shorter ways to do this but here's an expanded example.

http://sandbox.onlinephpfunctions.com/code/04507d4c64b2b0d53c6f490b1484e4b4bf54b9b3

Edit - added logic to replace empty value with 'Empty Data'

<?php
$output = [];

$input = "Data1    Data2    Data3    Data4    Data5    Data6    \nData8    Data9    Data10    Data11    Data12    Data13    Data14\n";

// The preg_split with PREG_SPLIT_NO_EMPTY will ignore any empty lines.
// http://php.net/manual/en/function.preg-split.php
foreach (preg_split('/\n/', $input, -1, PREG_SPLIT_NO_EMPTY) as $line) {

    $pieces = explode('    ', $line);

    // Loop over pieces and check if it is empty. If so, replace it with a string.
    $output[] = array_map(function($value) {
       return trim($value) === '' ? 'Empty Data' : $value;
    }, $pieces);
}

print_r($output);

Comments

0

It seems you want to split each record by a line delimiter. In that case, do that directly instead of joining all records together before splitting them apart.

Also, you can explode on 4 spaces directly instead of converting to a comma.

foreach (preg_split("/((\r?\n)|(\r\n?))/", $_GET['query']) as $line) {
    $info = explode ('    ', $query);
    $result = array_map('trim', $info);
    print_r(array_chunk($result, 7));
}

Comments

0

You can try this:

$input = "Data1    Data2    Data3    Data4    Data5    Data6    Data7
Data8    Data9    Data10    Data11    Data12    Data13
Data15    Data16    Data17    Data18    Data19    Data20    Data21";

parserInputData(explode("\n", $input));

function parserInputData(array $lines){
    $output = [];

    foreach ($lines as $line) {
        $query  = $line;
        $query  = str_replace('    ', ',', $query);
        $query  = str_replace("\n", ',', $query);
        $info   = explode (',', $query);
        $result = array_map('trim', $info);
        $columnsCount = count($result);

        if( $columnsCount < 7 ){
            $diff   = 7 - $columnsCount;
            $result = array_merge($result, array_fill($columnsCount-1, $diff, 'Empty data'));
        }

        $output[] = $result;
    }

    print_r($output);
}

The output for this code is

Array
(
    [0] => Array
        (
            [0] => Data1
            [1] => Data2
            [2] => Data3
            [3] => Data4
            [4] => Data5
            [5] => Data6
            [6] => Data7
        )

    [1] => Array
        (
            [0] => Data8
            [1] => Data9
            [2] => Data10
            [3] => Data11
            [4] => Data12
            [5] => Data13
            [6] => Empty data
        )

    [2] => Array
        (
            [0] => Data15
            [1] => Data16
            [2] => Data17
            [3] => Data18
            [4] => Data19
            [5] => Data20
            [6] => Data21
        )

)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.