I'm trying to properly "wordwrap" a given string into English plaintext. Take this example string:
This here is an example of what I'm talking about. Notice how I just talk nonsense on and on for no reason other than to push the 80-character line limit. And this is some more text, etc.
Please note: in the following examples, I have added underscores to visualize what I'm talking about. Naturally, the underscores are not added in reality. They are only here to make it clear what is happening.
If I simply blindly add a linebreak after each 80 chars, I get:
This here is an example of what I'm talking about. Notice how I just talk nonsen
se on and on for no reason other than to push the 80-character line limit. And t
his is some more text, etc._____________________________________________________
If I use the built-in wordwrap() function with 80 chars, I get:
This here is an example of what I'm talking about. Notice how I just talk_______
nonsense on and on for no reason other than to push the 80-character line limit.
And this is some more text, etc.________________________________________________
Neither of those look good or resemble a proper book or magazine, which either (depending on their age) have used software or humans to beautifully typeset them, like this:
This here is an example of what I'm talking about. Notice how I just talk nonse-
nse on and on for no reason other than to push the 80-character line limit. And_
this is some more text, etc.____________________________________________________
Notice how "nonsense" has neither been hard-cut or fully dropped on the next line. Instead, it has fully utilized the line minus one character for the dash, continuing on the next line. (As for the "And" in the end of the second line, it does have an whitespace after it, but only because there is only one more character left on that line.)
The rules for doing this kind of "intelligent wordwrapping" are language-specific, locale-specific (I think) and very complex. As such, it would be madness for me to attempt to code in all the rules manually.
I strongly suspect that there is some kind of mature, popular PHP library for doing precisely this, and I further suspect that it supports all kinds of languages/locales. However, I have been unable to find it myself.
It is not a requirement that it has to support "all kinds of languages/locales", but it would be nice. English with either US or UK locale would be sufficient for me to be happy at the moment.
I hope that I've been crystal-clear about what I'm asking!