Parsing html form textarea with php?

Caporegime
Joined
12 Mar 2004
Posts
29,919
Location
England
So I have a html form that is a textarea and I want to parse the input using php when it's submitted.

The textarea can be any number of rows and columns like this.

Code:
Spodumain    2,162    Spodumain            34,592 m3
Bright Spodumain    67    Spodumain            1,072 m3
Gecko    21    Combat Drone            1,050 m3
250mm Railgun II    6    Hybrid Weapon    Medium    High    60 m3
Strip Miner I    2    Strip Miner        High    50 m3
What I want to do is to look at each row, and take all the characters from the row and put it into a string, until I hit a number in the row, then take all the numbers (ignoring commas) and put that into a string until I hit whitespace.

Then I can see if each word string is in the database and if so take the value associated with it in the database and multiply it by the number I've taken from the row, to create a calculator of sorts.

In JSP this was pretty easy just using a java class to do it, but I'm not sure what functions to use in php?
 
Man of Honour
Joined
13 Nov 2009
Posts
11,607
Location
Northampton
Will there be a set amount of white space between the columns? Easiest way to do this would using the explode function. Something similar to the following, the syntax might be a little off but the theory is there. You want to explode the input by new lines then loop through that array exploding each row into its columns.

Code:
<?php

$input_text = $_POST['someInput'];

$input_rows = explode(PHP_EOL, $input_text);

$i = 0;

    foreach ( $input_rows as $row ) {
        $row[$i] = explode('\t',$row);
        $i;
    }
?>
 
Back
Top Bottom