wp_trim_words()
wp_trim_words( string $text, int $num_words = 55, strin…
wp_trim_words( string $text, int $num_words = 55, string $more = null )
将文本裁剪为一定数量的单词。
Trims text to a certain number of words.
目录锚点:#说明#参数#源码#笔记
说明(Description)
此函数已本地化。对于按单个字符计算“单词”的语言(如东亚语言),$num_words参数将应用于单个字符的数量。
参数(Parameters)
参数 | 类型 | 说明 |
---|---|---|
$text | (string) | 要修剪的文本。 |
$num_words | (int) | 字数。 |
$more | (string) | 如果需要修剪$text,要附加什么。默认“…”。 |
源码(Source)
/** * Trims text to a certain number of words. * * This function is localized. For languages that count 'words' by the individual * character (such as East Asian languages), the $num_words argument will apply * to the number of individual characters. * * @since 3.3.0 * * @param string $text Text to trim. * @param int $num_words Number of words. Default 55. * @param string $more Optional. What to append if $text needs to be trimmed. Default '…'. * @return string Trimmed text. */ function wp_trim_words( $text, $num_words = 55, $more = null ) { if ( null === $more ) { $more = __( '…' ); } $original_text = $text; $text = wp_strip_all_tags( $text ); /* * translators: If your word count is based on single characters (e.g. East Asian characters), * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'. * Do not translate into your own language. */ if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && preg_match( '/^utf-?8$/i', get_option( 'blog_charset' ) ) ) { $text = trim( preg_replace( "/[ ]+/", ' ', $text ), ' ' ); preg_match_all( '/./u', $text, $words_array ); $words_array = array_slice( $words_array[0], 0, $num_words + 1 ); $sep = ''; } else { $words_array = preg_split( "/[ ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY ); $sep = ' '; } if ( count( $words_array ) > $num_words ) { array_pop( $words_array ); $text = implode( $sep, $words_array ); $text = $text . $more; } else { $text = implode( $sep, $words_array ); } /** * Filter the text content after words have been trimmed. * * @since 3.3.0 * * @param string $text The trimmed text. * @param int $num_words The number of words to trim the text to. Default 5. * @param string $more An optional string to append to the end of the trimmed text, e.g. …. * @param string $original_text The text before it was trimmed. */ return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text ); }
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
3.3.0 | wp-includes/formatting.php | 15 | 7 |
笔记(Notes)
剥离格式的示例:
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!