tags:

views:

134

answers:

6

If I have a textarea like the one I'm using to type this message. I would like to append or concat a '>' to every line that breaks. The problem I am having is that I don't know where the lines break. What I want to do is emulate an email message when someone replies, they see '>' appended to every line. Is there a function for this?

Thanks.

A: 

How about replacing all line-breaks with a line-break and a greather-than? Something like this perhaps:

$subject = str_replace (PHP_EOL, PHP_EOL.'>', $subject);
Svish
Hi Svish. I just tied a string into that but it doesn't concat anything.
It doesn't concat anything. It puts a '>' after every linebreak in the $subject string, like you asked for.
Svish
+4  A: 

If it's for an email message, you could take a string and use the wordwrap function to break at about 75 chars:

$reply='> '.wordwrap($original, 75, "\n> ");

Because you can supply your own break string, you can include the > right there!

If the original is already broken into lines, then simply replace existing line break:

$reply='> '.str_replace ("\n", "\n >", $original);
Paul Dixon
Hey Paul, I'm gonna give this a shot. I think this may work. brb.
Genius. Thanks a lot bro!
Should maybe use PHP_EOL instead of \n?
Svish
Thanks Svish. Paul?? What do you think?
If it's for an email, \n is fine (we don't necessarily want the *system's* idea of what a line break is)
Paul Dixon
Ok, fair enough. Thanks again for all the help, guys.
Don’t forget the `>` in the first line.
Gumbo
good point, amended.
Paul Dixon
A: 

You can use the str_replace() function in PHP.

Mr. Smith
A: 

You mean on the client side? You'll need to do that in javascript.

Noon Silk
Stewart
Yes, it's obvious now that he's confirmed an answer; but lots of new programmers don't understand the different between server and client languages; and the fact that he said 'textarea' (which doesn't exist at the server side) indicated to me that it was clientside. I was wrong. No big deal.
Noon Silk
A: 

A little better for email quoting is:

$text = $_POST['text'];
$text = str_replace("\r\n", "\n", $text);
$text = str_replace("\r", "\n", $text);

$text = str_replace("\n", "\n> ", $text);
$text = wordwrap($text, 75, "\n> ");
$text = str_replace("\n> >", "\n>>", $text);
$text = ($text[0] == '>' ? '>' : '> ') . $text;
$text = htmlspecialchars($text);

This adds the always-useful space between the '>' and the text, while not bloating it by adding spaces between multiple quote levels.

Even better is if you can make it not wordwrap lines that are already quoted in the original message. Better still, implement format=flowed. But either of these'll take considerably more code.

Stewart
A: 

First off, thanks to Paul Dixon - great and simple idea :) Anyway I had some two problems with the solution: empty lines and lists (which have already \n linebreaks) wouldn't begind with > and long links are not cut after the given number of charaters.

So here is my extension to Pauls idea:

$reply = wordwrap($original, 75, "\n", true);
$reply = '> '.str_replace ("\n", "\n> ", $reply);

The first line wraps $original to 75 chars, breaks with \n and forces breaking of long words (ie: urls). The second line replaces the line breaks with a break and the > and adds an extra > to the beginning of the whole string.

Maybe this could be done easier but it works for me ;)

lorem monkey