Michael van Laar Posted August 2, 2015 Share Posted August 2, 2015 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 ;-) 4 Link to comment Share on other sites More sharing options...
Nico Knoll Posted August 7, 2015 Share Posted August 7, 2015 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. 1 Link to comment Share on other sites More sharing options...
Michael van Laar Posted August 8, 2015 Author Share Posted August 8, 2015 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. 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. 3 Link to comment Share on other sites More sharing options...
kixe Posted September 8, 2015 Share Posted September 8, 2015 $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. 1 Link to comment Share on other sites More sharing options...
Michael van Laar Posted September 11, 2015 Author Share Posted September 11, 2015 Ah, great That reduces the whole stuff to just one line. So simple. Did I already say that my RegEx know-how is very limited ;-) 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now