寻找“换行符”在文本区域中,这是自动换行阿拉伯语文本
我有一串文本显示在文本区域中(从右到左方向)。用户可以动态调整文本区域的大小(我使用jquery)并且文本将根据需要换行。
当用户点击提交时,我将获取该文本并使用 PHP 创建图像,但在提交之前我想知道“换行符”或“自动换行”发生在哪里。
到目前为止我看过的所有地方都只向我展示了如何在 php 端处理换行符。我想澄清的是,没有换行符。我拥有的是一个长字符串,它将根据用户设置的文本区域的宽度以不同的方式进行换行。
我无法使用“列”或任何其他标准宽度表示形式,因为我有一种非常复杂的阿拉伯字体,它实际上由许多不同宽度的字形(字符)组成。
如果有人知道访问自动换行发生位置的方法(如果需要,可以在文本区域或 div 中),我真的很想知道。
我唯一的其他解决方案是实际存储(在我的数据库中)每个字符的宽度(有点乏味,因为 600 种不同字体中有 200 多个字符,总共......一些巨大的数字)。
我的希望不高,但我想我会问。
谢谢
我。贾马尔
I have a string of text that I display in a textarea (right-to-left orientation). The user can resize the textarea dynamically (I use jquery for this) and the text will wrap as necessary.
When the user hits submit, I will take that text and create an image using PHP, BUT before submitting I would like to know where the "line-breaks" or rather "word-wraps" occur.
Everywhere I have looked so far only shows me how to process line-breaks on the php side. I want to make it clear that there ARE NO LINE-BREAKS. What I have is one LONG string that will be word-wrapped in different ways based on the width of the textarea set by the user.
I can't use "columns" or any other standard width representation because I have a very complex arabic font that is actually composed of glyphs (characters) of numerous different widths.
If anyone knows of a way of accessing where the word wraps occur (either in a textarea or a div if need-be), I'd really like to know.
My only other solution is to actually store (in my DB) the width of every single character (somewhat tedious since there are over 200 characters in 600 different fonts, for a total of...some huge number).
My hopes aren't high, but I thought I would ask.
Thanks
i. jamal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
好吧,您可以使用此函数强制将它们放入文本区域,而不是找到换行符(这实际上是不可能的):
此函数获取文本区域的ID,并且每当有自动换行,它将新换行符推入文本区域。运行表单提交中的函数,您将在服务器端代码中获得带有正确换行符的文本。
已在 IE、Chrome 和 Firefox 上成功测试,您可以在这里亲自查看:http://jsfiddle.net/ yahavbr/pH79a/1/ (预览将显示新行)
Well, instead of finding the line breaks (which is virtually impossible) you can force them into the textarea, using this function:
This function get ID of textarea and whenever there is word wrap, it push new line break into the textarea. Run the function in the form submit and you will get the text with proper line breaks in the server side code.
Tested successfully for IE, Chrome and Firefox feel free to see for yourself here: http://jsfiddle.net/yahavbr/pH79a/1/ (The preview will show the new lines)
下面是 Shadow Wizard 解决方案的功能等效实现,速度要快得多,因为它使用二分搜索而不是线性搜索来确定每行的长度:
更新了小提琴。
Here is a functionally-equivalent implementation of Shadow Wizard's solution that is much faster because it uses binary search instead of linear search to determine the length of each line:
Updated fiddle.
我认为最简单的方法是将 html 代码中文本区域的自动换行设置为“硬”,如下
所示 :提交时的字符串。如果您随后发现该字符串穿过您的字符串,则可以轻松确定最初的中断位置。或者你也可以使用 PHP 的 nl2br() 函数将这些字符转换为
html 标签。
I think the easiest way of doing it would be to set the word wrap of your textarea in the html code to 'hard', like this:
It means that wherever your textarea breaks the line for you will also insert a \n linebreak charachter in the string when submitting. If you then find this going through your string it will make it easy to determine where was the break originally. Or you can also turn these characters to
html tags with the nl2br() function of PHP.
出于某种原因,当这篇文章更新时,我从未收到过提醒……昨晚,我有了一个关于如何确定换行符的绝妙想法……
我会重建字符串,并每次检查宽度,它起作用了
所以我来这里分享它......发现我落后了 1 周,
无论如何 2 件重要的事情
您提供的代码使用了与我相同的绝妙想法(干得好)但是当我测试它时,它打破了第一个正确地行然后在每个字符后添加一个换行符(在链接 jsfiddle.net 上测试)
我添加了使用 jquery 的代码并使用跨度的宽度来确定何时换行
起初我尝试使用 div 的宽度,但 div.width() 返回默认宽度,而不是内容的宽度。
我知道这可能不适用于所有浏览器
所以,我恳请如果有人知道一种方法可以使这个万无一失,或者接近它,请分享。
首先,样式对于同步 textarea 和 div 之间的字体(所有属性)、设置大小以及(对于 IE)删除任何自动出现的滚动条是必需的。
接下来,我包括 jquery 和我的自定义函数:
最后,我的 html 测试页
For some reason, I was never alerted when this post was updated...and last night, I had this BRILLIANT idea on how to determine where the line breaks were...
I would rebuild the string, and check the width each time, and it WORKED
so I came here to share it...and found I was 1 week behind
Anyway 2 important things
The code you provided uses the same brilliant idea I had (well done you) BUT when I test it, it breaks the first line correctly then adds a line break after every character (tested on the link jsfiddle.net)
I've added my code which uses jquery and uses the width of a span to determine when to break
At first I tried using the width of the div, but div.width() returns the default width, not the width of the content.
I AM AWARE THIS MAY NOT WORK ON ALL BROWSERS
so, I ask kindly that if anyone knows of a way of making this foolproof, or close to it, please share.
First, the styles are necessary to synchornize fonts (all attributes) between the textarea and div, set the size, and (for IE) remove any scrollbars that automatically appear.
Next, I include jquery and my custom functions:
And finally, my html test page
这是我的示例,用于计算文本区域中每行文本的实际行数(换行后)。请注意,当滚动条开始出现时,文本区域的文本宽度会稍微缩小。这可能会导致先前内容进一步换行,从而导致先前计算的行高不正确。因此,文本区域必须将CSS样式的overflow-y设置为“滚动”,以强制始终显示滚动条。
上面的函数返回文本区域中每行文本(用“\n”分隔)的实际换行行数。
至少对于 Chrome 和 Firefox 来说,计算是准确的。
Here is my example for computing the actual number of rows (after line wrapping) for every line of text in a textarea. Take note that the text width of a textarea shrinks slightly when the scrollbar starts to appear. This can cause further wrapping of the previous content so that the previously computed row height will not be correct. Therefore, the textarea must have CSS-style overflow-y set to "scroll" to force the display of the scrollbar all the time.
The above function returns the actual number of wrapped rows for each line of text (separated by "\n") in an textarea.
The computation is accurate at least for Chrome and Firefox.
代码在“Crome”、“Firefox”和“IE”上进行了测试。获取换行符和回车符组件“textArea”(使用 JavaScript 的客户端)。
效果很好!
我想与您分享
重要的是包括风格
Code tested at "Crome", "Firefox" and "IE". Get line feeds and carriage returns a component "textArea" (client side with javascript).
WORKS WELL!.
I would like to share it with you
important to include the style
我无意挖掘旧帖子,但这是我搜索了一段时间后发现的。 @Shadow Wizard 的解决方案对我不起作用,因为它在每个句子后面都留了一个空格,所以我决定进行一些调整。我不是针对每个字符进行操作,而是针对每个单词进行操作。这使得检查更加容易,并且速度也快了 10 倍,使用完整的 Lorem Ipsum 检查(约 600 毫秒 vs 小提琴 6 秒,没有小提琴 120 毫秒)。
我创建了一个(已记录的)小提琴。我将看看是否可以优化它不过,多一点。
I don't mean to dig up old posts, but this is the one where I came out after searching for some time. @Shadow Wizard 's solution didn't work for me because it put a space behind every sentence, so I decided to make some adjustments. Instead of doing it per-character, I do it per-word. This results in way easier checks and is also 10 times faster, checking with full Lorem Ipsum (~600ms vs 6 full seconds on fiddle, 120ms without fiddle).
I have created a (documented) fiddle. I'm going to see if I can optimise it a bit more, though.