Michael van Laar

Textformatter: Thinspace Plus

Recommended Posts

Because I wanted to be able to strip all line breaks from a textarea field and then apply the Thinspace textformatter, I forked Netcarver’s module, added the required line and simply called it “Thinspace Plus” (which is not very creative, I know):

https://github.com/MichaelvanLaar/PW-TextformatterThinspacePlus

I use this Textformatter mostly for textarea fields that contain meta descriptions and similar content. The reason I use textarea fields here – instead of simple Text fields – is that I want to be able to use the great Textarea Counter module with these fields. But since these fields are intended to be simple strings without any line break or paragraph formatting, I want to “flatten” them – just in case an editor enters a line break. OK, the line breaks probably won’t do any harm in meta descriptions, OpenGraph descriptions, etc. But I just want to see them in my rendered source code ;-)

  • Like 4

Share this post


Link to post
Share on other sites
Nico Knoll    1,568

Do you replace double spaces afterwards to? Because a lot of people tend to make a space and than press enter (or Word does this and they just copy and paste it) :)


An maybe you could add a "maxlength" option and do a "nice break" so the last word won't be cut (as explained here: http://stackoverflow.com/questions/79960/how-to-truncate-a-string-in-php-to-the-word-closest-to-a-certain-number-of-chara).

This would make sense because meta tags have a length restriction I think.

  • Like 1

Share this post


Link to post
Share on other sites

I just added the “replace new line with a space character” before the two other rules of the original Thinspace module do their work:

    public function format(&$str)
    {
        $str = preg_replace('/\n/u', ' ', $str);
        $str = preg_replace('/(?:\x{20}|\x{a0}){2,}/u', ' ', $str);
        $str = preg_replace("/\n{2,}/u", "\n", $str);
    }

E. g. the combination “space + line break + space + line break” will be converted into “space + space + space + space” in a first step and then into a single “space” in a second step.
 
To have optimal content for meta elements which have a length limit, I create separate fields on a separate tab and use the Textarea Counter module.

post-531-0-83650800-1439051375_thumb.png

Of course i always include fallbacks in my templates. So if a description field is not filled with text, there are multiple fallback steps – ending with a “nice break” extract from the page’s main body text.

  • Like 3

Share this post


Link to post
Share on other sites
kixe    808
$value = trim(preg_replace('/\s+/', ' ', $value));

 I use this in some contact forms. Converts all single, multiple or mixed spaces, tabs, linefeeds and carriage-returns in a single space.

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.