0

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 ?
5
Contributors
4
Replies
37
Views
4 Years
Discussion Span
Last Post by diafol
1

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.

Edited by LastMitch: grammer

0
$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);

Edited by stekmil

0

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

0

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

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.