如何使用PHP将纯文本格式设置为书籍/杂志? [英] How to format plaintext like a book/magazine with PHP?

查看:112
本文介绍了如何使用PHP将纯文本格式设置为书籍/杂志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

This is a test string. Cool, huh?

我想要:

This is a te-
st string. C-
ool, huh?

也就是说,每行正好是13个字符, 根据该规则的英语规则 .也就是说,我不确定是否可以像"te-st"那样拆分"test",还是可以将"Cool"拆分成"C-ool",但这就是我要尝试的样式"实现.

我已经进行了大约一千次搜索查询.我什么都没找到.

wordwrap()是无用的,因为它仅适用于整个单词",并且在大多数行的末尾都留有大量空白.

这太令人沮丧了,因为直到我解决了这个问题,我才能继续我的项目.我以为会有一个库,但是我发现唯一甚至与远程相关的是 https://github. com/vanderlee/phpSyllable ,但这似乎根本没有做到这一点.该示例没有意义,因为它不显示任何输出,并且在任何地方都没有提及任何行宽".

解决方案

连字符 Org_Heigl/Hyphenator 似乎正在正确处理断字.基于其中之一,您应该能够编写自己的wordwrap(),以便将空格或连字符用作断点.

请注意,英语单词具有非常特定的连字符点,绝对不能保证每一行的长度都精确地 n 个字符.如果下一个音节恰好很长,有时您会缺少几个字符-例如,"though-"连字符表示为 thor-ough ,而"through"则根本不连字符. >

I have:

This is a test string. Cool, huh?

I want:

This is a te-
st string. C-
ool, huh?

That is, each line is exactly 13 chars, according to the English rules for this. That is, I'm not sure if "test" can be broken up like "te-st", or that "Cool" can be broken up to "C-ool", but that's the "style" I'm trying to achieve.

I've made about a thousand search queries. I find nothing whatsoever that does this.

wordwrap() is useless because it only works with entire "words" and leaves tons of blank space in the end of most lines.

This is so frustrating, because I can't continue my project until I've sorted this out. I assumed that there would be a library for this, but the only even remotely related I find is https://github.com/vanderlee/phpSyllable , but that doesn't seem to do this at all. The example makes no sense since it doesn't show any output and doesn't mention any line "width" anywhere.

解决方案

The libraries Hyphenator or Org_Heigl/Hyphenator seem to be handling hyphenation properly. Based on one of those, you should be able to write your own wordwrap() able to use either spaces or hyphens as break points.

Note that English words have very specific hyphenation points, and you're absolutely not guaranteed to have each line exactly n characters long. Sometimes you'll fall a few characters short, if the next syllable just happens to be long - for example "thorough" hyphenates as thor-ough, while "through" does not hyphenate at all.

这篇关于如何使用PHP将纯文本格式设置为书籍/杂志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆