Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split <texarea> field into individual lines. How? 1

Status
Not open for further replies.

josel

Programmer
Oct 16, 2001
716
US
I have a form where I use 'textarea' with rows='5' and cols='70' wrap=hard ... I intend to put form data to ASCII file on disk where it will be processed by an in-house application.

What I need to do is take the five rows and write them out as five lines in lieu of a single large field/line of text.

How can I do this?

Regards,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
That would depend on how you are going to define the end of line.

will your users just use the "enter" to designate the end of line? or will they probably just write everything down in a single line?


You can use explode to break up the text at the carriage return character, into an array. Each position of the array will contain one line of the textfield

Code:
$lines=explode("\n\r",$valuefromtextfield);

echo $lines[0];

will echo the first line of text

echo $lines[1];

 will echo the second line etc...

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
This is very cool ... thanks!



KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
@vacunita
do all browsers send a CRLF as a line break in a text area? even unix and macs? i thought there were some OS dependencies.

also should it not be "\r\n"?
 
In My experience, the order of \r\n is irrelevant. It works the same way in FF and IE6 for windows. Haven't tested it on Safari, so i'm not sure. I know Mac uses a CR instead of LF or \b\r but do you know if Safari for MAc sends a CR or LF jpadie? I would think FF would send the same characters in all its versions.






----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
my belief is that the order of \r\n absolutely matters. i had thought it would be like asking whether ab is the same as ba. I'm intrigued that you say it makes no difference and will investigate: it's possible that PHP does some auto-magical handling.

i'll also investigate the line termination thing. again: i had believed before your post that browsers adhered to operating system standards rather than their own implementations and that was why languages like PHP had constructs to analyse incoming data and perform conversions on the fly etc.
 

Wouldn't it be wiser and safer to use something like this ?

$lines = explode("<br />", nl2br($valuefromtextfield))
 
seldia: A Textarea will not send the <br/> html construct as a line terminator.

You are correct jpadie. I guess i wasn't paying attention to the results. The order does matter: "\r\n" And it works.

However I'd still like to know what other browsers, and OSes send for line terminators.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I'm not sure how you tested it Sleidia, but It doesn't work for me:

Try this:
Code:
<form action=returns.php method=POST>
<textarea name=lines cols=10, rows=5></textarea>
<input type=submit name=send value="sent">

<?
$break="<br/>";
if(isset($_POST['send'])){

$lines=explode($break,nl2br($_POST['lines']));

echo "The Lines are: <pre>";
print_r($lines);
echo "</pre><br> $lines[0]";

}
?>

I get this:

Code:
Array
(
    [0] => Line 1

Line 2

Line 3
)

Which meant no explosion happened on the string.

When it should be:

Code:
Array
(
[0] => Line 1
[1] => Line 2
[2] => Line 3

)


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
It works with no problems. The reason you couldn't get it to work, is because nl2br uses <br />, and not <br/>. Insert the extra space, and everything works perfectly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top