PHP 整洁和文本区域

发布于 2024-12-07 18:53:16 字数 1001 浏览 0 评论 0 原文

我正在使用 tidy 来清理和格式化由 twig 模板引擎

我使用以下配置来保持整洁:

$config = array('indent' => TRUE, 'output-html' => TRUE, 'wrap' => 0);

一切都运行良好,除了当我们到达文本区域时。

这是未清理的片段:

<textarea id="words"
         rows="10"       cols="50"                  >sdfds</textarea>

虽然格式非常混乱,但在文本区域中输出了正确的值:'sdfds'前后没有任何空格。

这是使用 tidy 后清理后的格式:

                <textarea id="words" name="words" rows="10" cols="50" title="prompt">
sdfds
</textarea>

可以看出,标记现在更加整洁,但是 tidy 在 'sdfds' 之后引入了换行符,这意味着在查看时光标现在指向 'sdfds' 之后的行浏览器。

这很烦人,我不知道如何处理这个问题。我仍然希望清理 textarea 标签,但我希望它的格式如下:

<textarea id="words" name="words" rows="10" cols="50" title="prompt">sdfds</textarea>

以前有人处理过这个问题吗?如果是这样,我怎样才能保持整洁,不为 textarea 标记引入这些空格?

I am using tidy to clean up and format the output of HTML generated by the twig template engine.

I am using the following configuration for tidy:

$config = array('indent' => TRUE, 'output-html' => TRUE, 'wrap' => 0);

Everything works nice and well, except when we get to textareas.

Here's the uncleaned fragment:

<textarea id="words"
         rows="10"       cols="50"                  >sdfds</textarea>

While the formatting is very messy, the correct value is outputted in the text area: 'sdfds' without any whitespace before or after.

This is the cleaned format after using tidy:

                <textarea id="words" name="words" rows="10" cols="50" title="prompt">
sdfds
</textarea>

As can be seen, the markup is much neater now, but tidy has introduced a linebreak after 'sdfds', which means that the cursor is now pointing at the line after 'sdfds' when viewed in the browser.

This is rather annoying, and I am not sure how to go about dealing with this. I would still want to have the textarea tag cleaned up, but I would prefer it to be formatted like so:

<textarea id="words" name="words" rows="10" cols="50" title="prompt">sdfds</textarea>

Has anyone dealt with this issue before? If so, How can I get tidy to not introduce those whitespaces for the textarea tag?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

七七 2024-12-14 18:53:16

如果 sdfds 使用 php 输出,您将需要添加另一个配置选项。

 $config = array('indent' => TRUE, 'output-html' => TRUE, 'wrap' => 0, 'wrap-php' => 0);

Wrap 只关心 html,任何 php 语句都被视为新行。有关配置选项的更多信息,您可以访问:http://tidy.sourceforge.net/docs/quickref .html

If sdfds is outputed with php you will need to add another config option.

 $config = array('indent' => TRUE, 'output-html' => TRUE, 'wrap' => 0, 'wrap-php' => 0);

Wrap only worries about html and any php statements are treated as a new line. For more information on config options you can visit: http://tidy.sourceforge.net/docs/quickref.html

只涨不跌 2024-12-14 18:53:16

当您使用缩进=>时确实,你会把德克萨斯州弄得一团糟。它变得整洁,因为即使是 textareas 值也是缩进的。如果您希望文本区域显示正确,您可以设置 indent =>错误的。这将整理您的 HTML,但在应用 tidy 后也会使您的文本区域保持相同的值。我看到有一些补丁可以解决这个问题,但是你应该自己编译整洁。您也可以使用 PHP 来完成此操作,但随后您就可以整理得井井有条了。

When you use indent => true you will get messed up texareas with tidy. It gets to tidy because even the textareas value is indented. If you want your textareas to appear correct you could just set indent => false. This will tidy up your HTML, but will also leave your textarea with the same value after applying tidy. I have seen there are some patches which solves the problem, but then you should compile tidy yourself. You could also do this with PHP but then you are tidying up tidy.

恏ㄋ傷疤忘ㄋ疼 2024-12-14 18:53:16

Tidy 有时可能会变得“混乱”,需要使用 rexep 进一步细化,这里有一个解决方法,可以将光标放在最终输出中的同一行上,因此可用性不会受到影响。只需运行你整理好的 html 槽:

$subject = preg_replace('%(\r\n|\n)(?=</textarea>)%sim', '', $subject);

Tidy can be rally a "messy" sometimes and needs further refinement with rexep, here is a workaround that will put the cursor on the same line in the final output, so the usability doesn't suffer. Just run your tidied html trough:

$subject = preg_replace('%(\r\n|\n)(?=</textarea>)%sim', '', $subject);
梦纸 2024-12-14 18:53:16

只需对文本区域使用修剪功能即可。你会得到你正在寻找的东西

Just use trim function for text area. And you will get what you are looking for

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文