Hello. I need to export the text typed into the text area and import it into an arraw.
but when i type in new lines it cant't read it.
example:when user types:
aaa
bbb
ccc
bbb
the out put at the end should be:
aaa
bbb
ccc
this is the code i tryed to do this, but it wont work.

$string_typed=$_GET['name']; //name is the id of text area
$replaced=str_replace("\n\r","\n",$string_typed);  //i want to replace new lines 
$exploded_string = explode("\n\r", $replaced); // the replaced string to insert into the array.
$print_string=array_unique($exploded_string ); // to remove the dublicated lines
foreach($aprint_string as $w){  //printig each of them.
    echo $w ."<br>";
}


// any ideo how can i realize it ?

Dani AI

Generated

Two quick things that will stop the script from ever producing the expected array: a newline mismatch and a small implementation/variable error.

The newline issue: browsers (especially on Windows) send CR+LF, not LF+CR, so the replace in the original post never matched the line endings. Also check the form control: PHP reads the form field by its name attribute (not the id), and using GET for large textarea content can hit URL-length limits. Finally, the foreach in the posted code uses the wrong variable name (the loop is over a misspelled array), which will throw or produce nothing.

A reliable, minimal approach (conceptually)

  • Submit the textarea with POST and ensure the textarea has a name attribute.
  • Normalize line endings on the server (convert any CRLF or lone CR to a single LF).
  • Split on LF to get lines, then trim each line.
  • Remove empty lines and remove duplicates while preserving the original order.
  • Reindex the array if you need sequential numeric keys before further processing or output.
  • Escape output for HTML (use htmlspecialchars) when printing to avoid XSS.

Debugging tips and cautions

  • Dump the raw string (var_dump) and inspect byte values if you're unsure which newline you have.
  • If duplicates should be case-insensitive, convert to a consistent case before dedupe.
  • array_unique preserves first occurrences but keeps original keys — use a reindex step if that matters.
  • For “any newline” matching, a PCRE-based approach that targets all newline flavors is more robust than single-string replaces.
  • Always escape user input when echoing to HTML.

Notes on the thread: correctly highlighted the delimiter problem; pointed out the regex/splitting idea for robustness; ’s answer demonstrates trimming/filtering. Fix the name/id, the foreach typo, and normalize newlines first — that will resolve the immediate problem.

Recommended Answers

All 4 Replies

Member Avatar for Member #949455

Hello. I need to export the text typed into the text area and import it into an array.

You just want to export the data from the textarea into the array?

Try this example (this example can transfer the data from the text area into the array):

You can modify the code from the link to suited your own code.

$string_typed=$_GET['name']; //name is the id of text area
$replaced=str_replace("\n\r","\n",$string_typed);  //i want to replace new lines 
$exploded_string = explode("\n", $replaced); // the replaced string to insert into the array.
$print_string=array_unique($exploded_string ); // to remove the dublicated lines
foreach($aprint_string as $w){  //printig each of them.
    echo $w ."<br>";
}

$replaced=str_replace("\n\r","\n",$string_typed);

"\n\r" -> "\n"

$exploded_string = explode("\n", $replaced);

Hi,

As per my understanding you need to make array for each new line. It is correct?
If yes then you can also use preg_split. (Split string by a regular expression)

$print_string = preg_split('/\r\n|\n\r|\r|\n/', $_GET['name'], -1, PREG_SPLIT_NO_EMPTY);

Please check and let me know.
Thanks,
Ajay

Member Avatar for Member #120589

Looks really complicated to me, how about?

<?php 
if($_GET && isset($_GET['arr']))
{
    $data = $_GET['arr'];
    $lines = explode("\n", $data);
    $clean = array_map("trim",$lines);
    $output = array_filter(array_unique($clean));
    print_r($output);
}
?> 
<!doctype html>
<html>
    <head> <title>Explode</title> </head> 
    <body>
        <form method = "get"> 
            <textarea name="arr">aaabbbccc</textarea>
            <input type = "submit" name = "Explode"/> 
        </form> 
    </body> 
</html> 

textarray default content there for convenience of testing

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.