How to format plaintext like a book/magazine with PHP?

前端 未结 2 1776
面向向阳花
面向向阳花 2021-01-28 04:20

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

2条回答
  •  执笔经年
    2021-01-28 05:09

    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.

提交回复
热议问题