system/Typography/Typography.php
public function formatCharacters(string $str): string
	{
		static $table;

		if (! isset($table))
		{
			$table = [
				// nested smart quotes, opening and closing
				// note that rules for grammar (English) allow only for two levels deep
				// and that single quotes are _supposed_ to always be on the outside
				// but we'll accommodate both
				// Note that in all cases, whitespace is the primary determining factor
				// on which direction to curl, with non-word characters like punctuation
				// being a secondary factor only after whitespace is addressed.
				'/\'"(\s|$)/'               => '&#8217;&#8221;$1',
				'/(^|\s|<p>)\'"/'           => '$1&#8216;&#8220;',
				'/\'"(\W)/'                 => '&#8217;&#8221;$1',
				'/(\W)\'"/'                 => '$1&#8216;&#8220;',
				'/"\'(\s|$)/'               => '&#8221;&#8217;$1',
				'/(^|\s|<p>)"\'/'           => '$1&#8220;&#8216;',
				'/"\'(\W)/'                 => '&#8221;&#8217;$1',
				'/(\W)"\'/'                 => '$1&#8220;&#8216;',
				// single quote smart quotes
				'/\'(\s|$)/'                => '&#8217;$1',
				'/(^|\s|<p>)\'/'            => '$1&#8216;',
				'/\'(\W)/'                  => '&#8217;$1',
				'/(\W)\'/'                  => '$1&#8216;',
				// double quote smart quotes
				'/"(\s|$)/'                 => '&#8221;$1',
				'/(^|\s|<p>)"/'             => '$1&#8220;',
				'/"(\W)/'                   => '&#8221;$1',
				'/(\W)"/'                   => '$1&#8220;',
				// apostrophes
				"/(\w)'(\w)/"               => '$1&#8217;$2',
				// Em dash and ellipses dots
				'/\s?\-\-\s?/'              => '&#8212;',
				'/(\w)\.{3}/'               => '$1&#8230;',
				// double space after sentences
				'/(\W)  /'                  => '$1&nbsp; ',
				// ampersands, if not a character entity
				'/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;',
			];
		}

		return preg_replace(array_keys($table), $table, $str);
	}
public function formatCharacters(string $str): string
	{
		static $table;

		if (! isset($table))
		{
			$table = [
				// nested smart quotes, opening and closing
				// note that rules for grammar (English) allow only for two levels deep
				// and that single quotes are _supposed_ to always be on the outside
				// but we'll accommodate both
				// Note that in all cases, whitespace is the primary determining factor
				// on which direction to curl, with non-word characters like punctuation
				// being a secondary factor only after whitespace is addressed.
				'/\'"(\s|$)/'               => '&#8217;&#8221;$1',
				'/(^|\s|<p>)\'"/'           => '$1&#8216;&#8220;',
				'/\'"(\W)/'                 => '&#8217;&#8221;$1',
				'/(\W)\'"/'                 => '$1&#8216;&#8220;',
				'/"\'(\s|$)/'               => '&#8221;&#8217;$1',
				'/(^|\s|<p>)"\'/'           => '$1&#8220;&#8216;',
				'/"\'(\W)/'                 => '&#8221;&#8217;$1',
				'/(\W)"\'/'                 => '$1&#8220;&#8216;',
				// single quote smart quotes
				'/\'(\s|$)/'                => '&#8217;$1',
				'/(^|\s|<p>)\'/'            => '$1&#8216;',
				'/\'(\W)/'                  => '&#8217;$1',
				'/(\W)\'/'                  => '$1&#8216;',
				// double quote smart quotes
				'/"(\s|$)/'                 => '&#8221;$1',
				'/(^|\s|<p>)"/'             => '$1&#8220;',
				'/"(\W)/'                   => '&#8221;$1',
				'/(\W)"/'                   => '$1&#8220;',
				// apostrophes
				"/(\w)'(\w)/"               => '$1&#8217;$2',
				// Em dash and ellipses dots
				'/\s?\-\-\s?/'              => '&#8212;',
				'/(\w)\.{3}/'               => '$1&#8230;',
				// double space after sentences
				'/(\W)  /'                  => '$1&nbsp; ',
				// ampersands, if not a character entity
				'/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;',
			];
		}

		return preg_replace(array_keys($table), $table, $str);
	}
Format Characters

This function mainly converts double and single quotes
to curly entities, but it also converts em-dashes,
double spaces, and ampersands

@param  string
@return string