WordPress как на ладони

convert_smilies()WP 0.71

Изменяет текстовые смайлики в тексте на соответствующие смайлики-картинки.

Функция работает, только если опция "использовать смайлики" включена и глобальные переменные (массивы) ($wp_smiliessearch, $wp_smiliesreplace) со смайликами не пустые.

Хуков нет.

Возвращает

Строку. отформатированный текст.

Использование

<?php convert_smilies( $text ) ?>
$text(строка) (обязательный)
Текст в котором нужно конвертировать смайлики.

Примеры

0

#1 Отформатируем текст:

$text = "Текст со смайликами :) :(";

echo convert_smilies( $text );

// выведет:
// Текст со смайликами <img src='/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  <img src='/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />

Заметки

  • Global. Строка|Массив. $wp_smiliessearch

Список изменений

С версии 0.71 Введена.

Код convert_smilies() WP 6.5.2

function convert_smilies( $text ) {
	global $wp_smiliessearch;
	$output = '';
	if ( get_option( 'use_smilies' ) && ! empty( $wp_smiliessearch ) ) {
		// HTML loop taken from texturize function, could possible be consolidated.
		$textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // Capture the tags as well as in between.
		$stop    = count( $textarr ); // Loop stuff.

		// Ignore processing of specific tags.
		$tags_to_ignore       = 'code|pre|style|script|textarea';
		$ignore_block_element = '';

		for ( $i = 0; $i < $stop; $i++ ) {
			$content = $textarr[ $i ];

			// If we're in an ignore block, wait until we find its closing tag.
			if ( '' === $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')[^>]*>/', $content, $matches ) ) {
				$ignore_block_element = $matches[1];
			}

			// If it's not a tag and not in ignore block.
			if ( '' === $ignore_block_element && strlen( $content ) > 0 && '<' !== $content[0] ) {
				$content = preg_replace_callback( $wp_smiliessearch, 'translate_smiley', $content );
			}

			// Did we exit ignore block?
			if ( '' !== $ignore_block_element && '</' . $ignore_block_element . '>' === $content ) {
				$ignore_block_element = '';
			}

			$output .= $content;
		}
	} else {
		// Return default text.
		$output = $text;
	}
	return $output;
}
2 комментария
    Войти