简体   繁体   English

文本区域换行不起作用

[英]Text-area wrapping not working

I have 我有

$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' . 表单method='POST'enctype='multipart/form-data'textarea wrap='hard'

I want the displayed text-area to look exactly the same as when the user entered the text in it. 我希望显示的文本区域看起来与用户在其中输入文本时完全相同。

UPDATE: 更新:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nl
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

OUTPUTS this: 输出结果:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Using wordwrap function in PHP. 在PHP中使用自动wordwrap功能。

It doesn't seem to make any difference whether I set wrap="hard" in HTML either. 是否在HTML中设置wrap="hard"似乎也没有什么区别。

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');

在这些情况下,您应该指定wordwrap()函数的第四个参数,如果它大于要求的宽度,则指示它打断单词:

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

The input string 输入字符串

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nl aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

contains 4 distinct characters: "a", "\\n", "l", and " ". 包含4个不同的字符:“ a”,“ \\ n”,“ l”和“”。

I would expect nl2br() to create this output 我希望nl2br()创建此输出

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br />\nl aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Then the wordwrap call makes this: 然后,自动换行调用使此操作:

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

This is ill-formed html. 这是格式不正确的html。 In the browser, the bad br tags have no effect. 在浏览器中,错误的br标签无效。 And the newlines don't affect the layout, so it looks like a string of 'a'. 而且换行符不会影响布局,因此它看起来像一个字符串“ a”。 I can't explain where your 'l' goes to. 我无法解释您的“ l”去哪里。

In your wordwrap call, the replacement string is in single quotes, so the '\\n' is two characters backslash and n. 在您的自动wordwrap调用中,替换字符串用单引号引起来,因此'\\ n'是两个字符反斜杠和n。 Use double quotes to expand the escape into a true newline: Change '<br />\\n' to "<br />\\n" 使用双引号将转义扩展为真正的换行符:将'<br />\\n'更改为"<br />\\n"

You may also want to leave out the either the nl2br call or the wordwrap call, depending on what you want. 您可能还想根据需要nl2br调用或wordwrap调用。

By the way, have you tried setting the fourth argument, $cut , to true in the wordwrap() call? 顺便说一句,您是否尝试在wordwrap()调用中将第四个参数$cut设置为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM