[英]Center align plain text (php)
I have been asked to come up with a solution which would allow our users to create their own custom javascript dialog text. 我被要求提出一个解决方案,该方案将允许我们的用户创建自己的自定义JavaScript对话框文本。 However we need it to be centered.
但是,我们需要将其居中。
At the moment, we have a textarea, which the user pads using spaces and tests with a preview button. 目前,我们有一个文本区域,用户可以使用空格进行填充,并使用预览按钮进行测试。 I would like to change this to allow for the text to be center aligned automatically.
我想更改此设置以允许文本自动居中对齐。 I think this would mean just adding the spaces myself line by line in the backend, and also adding in the correct line breaks.
我认为这意味着仅在后端逐行添加空格,并添加正确的换行符。
The only way I can think of doing it, is getting the longest line as int
, and then subtracting subsequent lines from it, and diving the result by two, and tacking that many spaces on front and back. 我能想到的唯一方法是,将最长的行作为
int
,然后从中减去后续的行,然后将结果除以2,然后在前面和后面加上很多空格。
Is there a cleaner more elegant way to approach this problem? 有没有更干净,更优雅的方法来解决此问题? Are there ways of aligning text actually inside the dialog?
对话框内部是否有对齐文本的方法?
I had considered something like TinyMCE, but I think it's a little overkill, for what is essentially a 150 character, 4-5 line string. 我曾经考虑过TinyMCE之类的东西,但我认为这有点过头了,因为它实际上是150个字符的4-5行字符串。
This page has a useful javascript function for center-aligning a string using padding. 此页面具有有用的javascript函数,用于使用填充使字符串居中对齐。 Assuming you're displaying plain fixed-width text (using a
<pre>
tag or similar) then you'll need to get the length of the longest line and pad accordingly. 假设您要显示纯正的固定宽度文本(使用
<pre>
标记或类似标记),则需要获取最长的行的长度并相应地填充。 If not, it's just a matter of setting the css: #myDiv { text-align:center; }
如果没有,那只是设置CSS的问题:
#myDiv { text-align:center; }
#myDiv { text-align:center; }
on the div containing the text. 包含文本的div上的
#myDiv { text-align:center; }
。
On the PHP side, you can do this. 在PHP方面,您可以执行此操作。
$lines=array();
foreach (explode("\n",wordwrap($str,$len=80)) as $line)
$lines[]=str_pad($line,$len,' ',STR_PAD_BOTH);
echo implode("\n",$lines);
The Javascript version should be easy to write. Javascript版本应该易于编写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.