tags:

views:

65

answers:

3
$ad_text=wordwrap(nl2br($_POST['annonsera_text']), 45, '<br />\n');

Any idea why the above does display in a long string?

Form method='POST', and enctype='multipart/form-data' and textarea wrap='hard'

I want the displayed text-area to look exactly the same as when the user entered the text in it... thanks...

UPDATE:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nl
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

OUTPUTS this:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Using wordwrap function in php...

It doesnt matter if I set the wrap="hard" in html either.

+1  A: 

The correct answer to this is going to be found in your debugging method. Instead of putting this into one line, separate these out into multiple lines and check the output.

echo $ad_text = $_POST['annonsera_text'];
echo $ad_text = nl2br($ad_text);
echo $ad_text = wordwrap($ad_text, 45, '<br />\n');
Citizen
+1  A: 

In these cases you should specify fourth parameter of wordwrap() function, which instructs it to break words, if they are larger than required width:

$ad_text=wordwrap(nl2br($_POST['annonsera_text']), 45, '<br />\n', true);
Kaspars Foigts
how could i be so blind... thanks!
Camran
A: 

The input string

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nl aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

contains 4 distinct characters: "a", "\n", "l", and " ".

I would expect nl2br() to create this output

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br />\nl aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Then the wordwrap call makes this:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br<br />\n/>\nl aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

This is ill-formed html. In the browser, the bad br tags have no effect. And the newlines don't affect the layout, so it looks like a string of 'a'. I can't explain where your 'l' goes to.

In your wordwrap call, the replacement string is in single quotes, so the '\n' is two characters backslash and n. Use double quotes to expand the escape into a true newline: Change '<br />\n' to "<br />\n"

You may also want to leave out the either the nl2br call or the wordwrap call, depending on what you want.

By the way, have you tried setting the fourth argument, $cut, to true in the wordwrap() call?

Ewan Todd