wptexturize

Definition:
function wptexturize($text) {}

Replaces common plain text characters into formatted entities
As an example,

Parameters

  • string $text: The text to be formatted

Return values

returns:The string replaced with html entities

Defined filters

  • no_texturize_tags
    apply_filters('no_texturize_tags', $default_no_texturize_tags)
  • no_texturize_shortcodes
    apply_filters('no_texturize_shortcodes', $default_no_texturize_shortcodes)

Source code

function wptexturize($text) {

	global $wp_cockneyreplace;

	static $opening_quote, $closing_quote, $en_dash, $em_dash, $default_no_texturize_tags, $default_no_texturize_shortcodes, $static_characters, $static_replacements, $dynamic_characters, $dynamic_replacements;



	// No need to set up these static variables more than once

	if ( empty( $opening_quote ) ) {

		/* translators: opening curly quote */

		$opening_quote = _x('“', 'opening curly quote');

		/* translators: closing curly quote */

		$closing_quote = _x('”', 'closing curly quote');

		/* translators: en dash */

		$en_dash = _x('–', 'en dash');

		/* translators: em dash */

		$em_dash = _x('—', 'em dash');



		$default_no_texturize_tags = array('pre', 'code', 'kbd', 'style', 'script', 'tt');

		$default_no_texturize_shortcodes = array('code');



		// if a plugin has provided an autocorrect array, use it

		if ( isset($wp_cockneyreplace) ) {

			$cockney = array_keys($wp_cockneyreplace);

			$cockneyreplace = array_values($wp_cockneyreplace);

		} else {

			$cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");

			$cockneyreplace = array("’tain’t","’twere","’twas","’tis","’twill","’til","’bout","’nuff","’round","’cause");

		}



		$static_characters = array_merge( array('---', ' -- ', '--', ' - ', 'xn–', '...', '``', '\'\'', ' (tm)'), $cockney );

		$static_replacements = array_merge( array($em_dash, ' ' . $em_dash . ' ', $en_dash, ' ' . $en_dash . ' ', 'xn--', '…', $opening_quote, $closing_quote, ' ™'), $cockneyreplace );



		$dynamic_characters = array('/\'(\d\d(?:’|\')?s)/', '/\'(\d)/', '/(\s|\A|[([{<]|")\'/', '/(\d)"/', '/(\d)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A|[([{<])"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/\b(\d+)x(\d+)\b/');

		$dynamic_replacements = array('’$1','’$1', '$1‘', '$1″', '$1′', '$1’$2', '$1' . $opening_quote . '$2', $closing_quote . '$1', '’$1', '$1×$2');

	}



	// Transform into regexp sub-expression used in _wptexturize_pushpop_element

	// Must do this everytime in case plugins use these filters in a context sensitive manner

	$no_texturize_tags = '(' . implode('|', apply_filters('no_texturize_tags', $default_no_texturize_tags) ) . ')';

	$no_texturize_shortcodes = '(' . implode('|', apply_filters('no_texturize_shortcodes', $default_no_texturize_shortcodes) ) . ')';



	$no_texturize_tags_stack = array();

	$no_texturize_shortcodes_stack = array();



	$textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);



	foreach ( $textarr as &$curl ) {

		if ( empty( $curl ) )

			continue;



		// Only call _wptexturize_pushpop_element if first char is correct tag opening

		$first = $curl[0];

		if ( '<' === $first ) {

			_wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>');

		} elseif ( '[' === $first ) {

			_wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']');

		} elseif ( empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack) ) {

			// This is not a tag, nor is the texturization disabled static strings

			$curl = str_replace($static_characters, $static_replacements, $curl);

			// regular expressions

			$curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);

		}

		$curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&$1', $curl);

	}

	return implode( '', $textarr );

}

3411

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: