Single-letter words at the end of line

Is there some Kirby solution for this problem?

Thanks

What do you need exactly? I can’t really tell from the title alone.

Guess to achieve that you’d have to insert non-breaking spaces after every single letter word. As far as I can see, there is no method in Kirby that does that. But you could create a custom field method.

Yes thats my problem, thanks for clarification :slight_smile:
Can you share some links where I can find more help with this?

Thanks a lot!

Maybe the widon't() method could be a starting point for your own.

1 Like

Here is my first try, maybe useful for someone.
I need test it within some Kirby tags.

Credit: https://stackoverflow.com/questions/2094751/php-regex-replace-white-space-by-nbsp-if-it-is-following-single-letter

Kirby::plugin('plugin/name', [
    'fieldMethods' => [
        'widows' => function ($field) {
            $s = explode(' ', $field->value);

            foreach ($s as $i => $j) {
                if ((strlen($j) == 1 || is_numeric($j)) && !ctype_punct($j)) {
                    $s[$i] = $j . ' ';
                } else {
                    if ($i < count($s) - 1) {
                        $s[$i] = $j . ' ';
                    } else {
                        $s[$i] = $j . '';
                    }
                }
            }

            return implode('', $s);
        }
    ]
]);