0

I am reading from files and each line has multiple spaces. Example:

$line = 'Testing Area                               1  10x10';

I need to convert it into array with 3 elements only so I can save it to a table. Final output should be like this:

$final_array = array('Testing Area', '1', '10x10');

This is how I'm doing it so far:

// read by line
foreach(explode(PHP_EOL, $contents) as $line) {

    // split line by 2 spaces coz distance between `1` and `10x10` is atleast 2 spaces
    $arr = explode('  ', $line);

    // But because `Testing Area` and `1` has so many spaces between them,
    // exploding the $line results to empty elements.
    // So I need to create another array for the final output.

    $final_array = array();

    // loop through $arr to check if value is empty
    // if not empty, push to $final array
    foreach ($arr as $value) {
        if (!empty($value)) {
            array_push($final_array, $value);
        }
    }

    // insert into table the elements of $final_array according to its column
}

Is there a better way to do this instead of looping through the array and checking each element if it's empty? Take note that I have multiple files to read, each containing atleast 200 lines like that.

rhemmuuu
  • 1,147
  • 2
  • 5
  • 19
  • @executable He wants 2 or more, not 1 or more. – Barmar Mar 30 '22 at 08:43
  • First give a good definition of what you are trying to accomplice. Is it always 3 elements? And the last two will not contain any white spaces? And does the number of spaces matter or is it 1-or-more? – Ivo P Mar 30 '22 at 08:45
  • @IvoP it's not always 3 elements but it always has atleast two spaces in between. Barmar's answer is the simplest way to do it. – rhemmuuu Mar 30 '22 at 08:51

2 Answers2

1

Assuming the criteria for splitting be two or more spaces, we can try using preg_split here:

$line = 'Testing Area                               1  10x10';
$final_array = preg_split("/\s{2,}/", $line);
print_r($final_array);

This prints:

Array
(
    [0] => Testing Area
    [1] => 1
    [2] => 10x10
)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Please close duplicates instead of answering them. [The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place.](https://stackoverflow.com/help/duplicates#:~:text=The%20fundamental%20goal%20of%20closing%20duplicate%20questions%20is%20to%20help%20people%20find%20the%20right%20answer%20by%20getting%20all%20of%20those%20answers%20in%20one%20place.) – mickmackusa Jul 09 '22 at 13:07
1

Use preg_split(), with 2 or more spaces as the delimiter.

$array = preg_split('/\s{2,}/', $line);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • If you can resolve a question with one sentence and one line of code, it is very likely to be a duplicate in 2022. [The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place.](https://stackoverflow.com/help/duplicates#:~:text=The%20fundamental%20goal%20of%20closing%20duplicate%20questions%20is%20to%20help%20people%20find%20the%20right%20answer%20by%20getting%20all%20of%20those%20answers%20in%20one%20place.) – mickmackusa Jul 09 '22 at 13:06
  • Is it just me, or do you badger everyone by finding cases in the past where they could have found a duplicate? I'm pretty good at finding duplicates, but some things are notoriously difficult to search for. – Barmar Jul 09 '22 at 16:06