You are here

public static function Typogrify::widont in Typogrify 8

Widont.

Replaces the space between the last two words in a string with `` `` Works in these block tags ``(h1-h6, p, li)`` and also accounts for potential closing inline elements ``a, em, strong, span, b, i``

Empty HTMLs shouldn't error

Parameters

string $text: The text to work on.

Return value

string The modified text.

4 calls to Typogrify::widont()
Typogrify::filter in src/TwigExtension/Typogrify.php
Filter text by Typogrify.
Typogrify::filter in src/Typogrify.php
Typogrify.
TypogrifyClassTest::testWidont in tests/src/Unit/TypogrifyClassTest.php
Widont test.
TypogrifyFilter::process in src/Plugin/Filter/TypogrifyFilter.php
Performs the filter processing.

File

src/Typogrify.php, line 186

Class

Typogrify
Class \Drupal\typogrify\Typogrify.

Namespace

Drupal\typogrify

Code

public static function widont($text) {

  // This regex is a beast, tread lightly.
  $widont_finder = "/([^<>\\s]+|<\\/span>)                    # ensure more than 1 word\n                      (\\s+)                                   # the space to replace\n                      ([^<>\\s]+                               # must be flollowed by non-tag non-space characters\n                      \\s*                                     # optional white space!\n                      (<\\/(a|em|span|strong|i|b)[^>]*>\\s*)*   # optional closing inline tags with optional white space after each\n                      ((<\\/(p|h[1-6]|li|dt|dd)>)|\$))          # end with a closing p, h1-6, li or the end of the string\n                      /x";
  return preg_replace($widont_finder, '$1&nbsp;$3', $text);
}