-1

So, I am making a tool for users that want to copy / paste lines of text from excel into a text box. The goal is for each line to be a seperate variable. If possible I'd like to count the lines and turn them into arrays.

<form method="post" action="/TestThisOneOut2.php">
  <label for="CompetitorPartNumber">Competitor Part Number:</label><br>
  <textarea rows="4" id="CompetitorPartNumber" name="CompetitorPartNumber1" cols="50" name="comment">
Test</textarea><br>
  <input type='submit' value='Download Now' class="SubmitButton">

For example a user might enter the following into a text box,

X5044MLGSP4
7PM985DSA
PO94ASDS

I would have no way of knowing if they want to enter 1 line, 3 lines or 300 lines of product codes, but I'd like to make one text box that will automatically split itself apart. The goal would be to have 1 text area text box that users would enter info into.

In the above case I am struggling with a way to programatically identify that there are 3 seperate lines and therefore 3 variables needed. I was hoping there was some way to do it off of a carriage return or something.

Then for the above example: I'd like them to go to an array for example:

$CompetitorPartNumber[0] = $_POST['CompetitorPartNumber1'];
$CompetitorPartNumber[1] = $_POST['CompetitorPartNumber2'];
$CompetitorPartNumber[2] = $_POST['CompetitorPartNumber3'];

Then I can use the data to do database lookups and return data. Thanks for your help.

I have tried the following

<form method="post" action="/TestThisOneOut2.php">
  <label for="CompetitorPartNumber">Competitor Part Number:</label><br>
  <textarea rows="4" id="CompetitorPartNumber" name="CompetitorPartNumber" cols="50" name="comment">
Test</textarea><br>
  <input type='submit' value='Download Now' class="SubmitButton">
  </form>  

And it sends the posted data to the following script:

<?php 
$CompetitorPartNumber = $_POST['CompetitorPartNumber'];

echo "<h1>".$CompetitorPartNumber."</h1>";
?>

So the problem I have is that when I echo the results they appear as blank spaces in place of the items being on seperate lines. This is good unless a user actually enters a blank space on accident. I was thinking I could could the blank spaces to make the number of fields in an array. Then split up the variable by blank space character. However, users may enter blank spaces at the end so I don't think this will be a good strategy as I may get empty results. I was hoping I could ask around for code samples incase someone else knew how to solve this problem.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Use `explode()` to split the input into an array of lines. – Barmar Aug 15 '23 at 21:14
  • Thanks for the edit to make the code look better formatted. I would use explode(), however, I need something as a separator. I wondered if there was a way to use it against carriage returns or to inject some special character at the end of each line or something. I would use spaces, but there is a high likelihood that a user will accidentally add a space. – GuyTryingToSolveAnIssue Aug 15 '23 at 21:17
  • 1
    Use newline as the separator: `explode("\n", $_POST['CompetitorPartNumber')` – Barmar Aug 15 '23 at 21:19
  • Oh, that sounds extremely promising. I will give it a try. – GuyTryingToSolveAnIssue Aug 15 '23 at 21:20
  • 1
    Do you have to use a textarea? Why not use `` and allow the user to add and remove them dynamically? – Barmar Aug 15 '23 at 21:21
  • Nope, I could use and make it look very large. It's been requested that I create a text box though. So as long as it looks like a text box and acts like a text box, the specific input type doesn't matter. – GuyTryingToSolveAnIssue Aug 15 '23 at 21:25
  • I'm not suggesting that you make it very large, I'm suggesting that you use multiple text inputs, one for each part number. Then you don't need to do any parsing, they'll already be in an array. – Barmar Aug 15 '23 at 21:28
  • Now that I know about /n I can used echo substr_count("$TestString","/n"); to get a count of the new lines. I can use this to get the count for my array. Then I can use your idea of using explode($TestString,"/n") to actually split the text into chunks. Next, I can use a while loop to assign each text to the array. That, "\n" was the key I was looking for. – GuyTryingToSolveAnIssue Aug 15 '23 at 21:31
  • 1
    it's `\n` not `/n` – Barmar Aug 15 '23 at 21:31
  • Thank you for your help Barmar, you've pretty much solved this for me. – GuyTryingToSolveAnIssue Aug 15 '23 at 21:32

2 Answers2

1

the form

<?php
$CompetitorPartNumbers = $_POST['CompetitorPartNumbers'] ?? 'Test';
    
?>
<form method="post" action="">
  <label for="CompetitorPartNumber">Competitor Part Number:</label><br>
  <textarea rows="4" id="CompetitorPartNumber" name="CompetitorPartNumbers" cols="50" name="comment">
<?= $CompetitorPartNumbers ?></textarea><br>
  <input type='submit' value='Download Now' class="SubmitButton">

the target script

<?php
$CompetitorPartNumbers = $_POST['CompetitorPartNumbers'] ?? 'Test';
echo "<h1>plain input</h1>";
echo "<pre>".$CompetitorPartNumbers."</pre>";
    
// split by new line
$CompetitorPartNumbersEx = explode("\n", $CompetitorPartNumbers);
// split by carriage return
$CompetitorPartNumbersEx = explode("\n\r", $CompetitorPartNumbers);
// split by The correct 'End Of Line' symbol for this platform.
$CompetitorPartNumbersEx = explode(PHP_EOL, $CompetitorPartNumbers);
?>
<h1><code>explode()</code> the values into lines</h1>
<pre><?= print_r($CompetitorPartNumbersEx, true) ?></pre>
<h2>count the lines</h2>
<samp><?='count = '.count($CompetitorPartNumbersEx) ?></samp>

output looks something like: screenshot of the output

yarns
  • 66
  • 8
  • I ended up doing a more complex version of this:


    – GuyTryingToSolveAnIssue Aug 16 '23 at 17:41
  • and Count: ".$count.""; while ($counter != $count) { echo "

    ".$CompetitorPartNumbers[$counter]."

    "; $counter++; } ?>
    – GuyTryingToSolveAnIssue Aug 16 '23 at 17:42
  • 1
    The concepts you gave me were extremely useful, @yarns. Thank you for your help. – GuyTryingToSolveAnIssue Aug 16 '23 at 17:42
  • 1
    No problem. I wanted to show how to split the submitted string with regular expressions (i.e. PCRE, `preg_split()`) but I think the `PHP_EOL` constant is more important. `PHP_EOL` (**PHP**_**E**nd **O**f **L**ine) will split the string into lines if those lines are separated by carriage return, new line, or return characters / code points. – yarns Aug 16 '23 at 22:36
0

For a HTML <textarea> element, you find the to its name attribute corresponding value in the $_POST array (<form> with the method=post attribute value).

The value is a string and lines are delimited by CR LF which is "\r\n" as string in PHP.

For example:

<?php

# ...

# Split the POST textarea value into lines:

/** @var string[] $CompetitorPartNumber */
$CompetitorPartNumber = explode("\r\n", $_POST['CompetitorPartNumber']);

# Trim & filter:

$CompetitorPartNumber = array_map('trim', $CompetitorPartNumber);

$CompetitorPartNumber = array_filter('strlen', $CompetitorPartNumber);

To convert the lines back for the textarea field:

<?php

# ...

$CompetitorPartNumberValue = implode("\r\n", $CompetitorPartNumber);

?>
<form ...>

  <textarea name="CompetitorPartNumber"
            cols="..." ...
            ><?= htmlspecialchars($CompetitorPartNumberValue); ?></textarea>

</form>

And if you're looking for some small script to extend for testing, see Simple PHP editor of text files, you can directly code along with the PHP development webserver from the command-line.

hakre
  • 193,403
  • 52
  • 435
  • 836