Text Area Line Break Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
locell
Forum Newbie
Posts: 3
Joined: Sat Oct 25, 2003 11:25 pm

Text Area Line Break Problem

Post by locell »

I'm gathering emails from a text area like so:

email1@server.com
email2@server.com
email3@server.com

When I try to print it out on the next page or split()
them nothing happends, and they come out like:
email1@server.com email2@server.com email3@server.com

What should I do?

Here is the code:

<?php
$leadmail_email_addresses2 = nl2br($leadmail_email_addresses);
$email_c = 0;


$emails_html = split("\r\n", $leadmail_email_addresses);
$email_c = count($emails_html);


?>
<?php echo($email_c); ?>
<?php echo($leadmail_email_addresses3); ?>
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Are you sure that "\r\n" is the correct delimiter?

To test, look at the nth and (n+1)th elements of the string and ord() them.

ord($string) will return the ascii value of the first character of $string. Compare that to an ascii table to see if it really is the carriage return.
daveheinzel
Forum Newbie
Posts: 8
Joined: Tue Dec 02, 2003 10:47 pm

Post by daveheinzel »

I had this same problem tonight, and I somewhere I found this nifty little thing:

nl2br()

When info is entered into my database using PHP, I don't treat line breaks. But when that data is retreived, nl2br() adds an html <br /> before the line break, reformatting the text to as it was entered.

I used it like this:

after you get your query results, do something like this:

echo nl2br("$results['emailaddresses']");

It worked for me, and I don't know much about PHP, so I'm sorry I can't elaborate.
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Post by mchaggis »

The problem is simple as is the solution:

Code: Select all

<?php
# Re-Format the new lines to standard unix format
$leadmail_email_addresses = ereg_replace("\r\n","\n",$leadmail_email_addresses);

# Create an Array of the emails
$emails_html = split("\n",$leadmail_email_addresses);

$email_c = count($emails_html); 

echo $email_c; 
echo nl2br($leadmail_email_addresses); 
# Or
echo join('<br />', emails_html);
?>
[edit of a typo --JAM ;)]
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post by mrvanjohnson »

I think mchaggis meant to say

Code: Select all

<?php
echo nl2br($leadmail_email_addresses);
?>
not

Code: Select all

<?php
echo nr2br($leadmail_email_addresses);
?>
Post Reply