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 ?
Member Avatar for LastMitch

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):

http://www.ittreats.com/os/php/text-area-values-into-array-in-php.html

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 diafol

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.