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

人走茶凉 提交于 2020-01-22 02:48:09

问题


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.


回答1:


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.




回答2:


Here's some code you should be able to work with, using some sort of hyphenation library. This code uses a fake 3-character hyphenation function. You can see it in action at https://www.tehplayground.com/eNtxiMTeXj16oPkT - it took me about 10 minutes to write this, so it was, in fact, trivial - contrary to what you wrote in the now deleted thread.

<?php
$loremipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

function FAKE_hyphenate($word) { // use some real library's hyphenation function here
    return explode("-",wordwrap($word,3,"-",true));
}

function hyphenate_text ($text, $line_length) {
    $words = explode(" ",$text);
    $lines = [str_repeat("-",$line_length)];
    $line = "";
    while ($words) {
        $word = array_shift($words);
        if (strlen($line)+strlen($word)+1<=$line_length)
            $line .= (strlen($line)>0 ? " " : "") . $word;
        else {
            $syllables = FAKE_hyphenate($word);
            $syllables[0] = " ".$syllables[0];
            $syl_count=0;
            while ($syllables) {
                $syllable = array_shift($syllables);
                if (strlen($line)+strlen($syllable)<=$line_length-1) {
                    $line .= $syllable;
                    $syl_count++;
                } else {
                    array_unshift($syllables,$syllable);
                    break;
                }
            }
            if ($syl_count>0)
                $line .= "-";
            $syllables[0] = str_replace(" ","",$syllables[0]);
            array_unshift($words,implode("",$syllables));
            $lines[] = $line;
            $line = "";
        }
    }
    $lines[] = $line;
    return implode("\n",$lines);
}

echo hyphenate_text($loremipsum,25);


来源:https://stackoverflow.com/questions/59699027/how-to-format-plaintext-like-a-book-magazine-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!