我试图修改wp_trim_words函数以返回左边的单词以及第一部分,任何帮助都非常感激。
function wp_trim_words_new( $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 (East Asian characters),
enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $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( "/[\n\r\t ]+/", $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 );
}
答案 0 :(得分:1)
在我看来,你只是复制了trim_words()
function of Wordpress。你本可以做得更好。您想要的输出不清楚。
假设您对使用原始函数的输出返回的简单无标记字符串的剩余字符串感到满意,您可以使用类似这样的内容(未经测试):
function wp_trim_words_2( $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 (East Asian characters),
enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
preg_match_all( '/\X/u', $text, $match_array );
$split_array = $match_array[0];
$sep = '';
} else {
$split_array = preg_split( "/[\n\r\t ]+/", $text, -1, PREG_SPLIT_NO_EMPTY );
$sep = ' ';
}
$words_array = array_slice( $split_array, 0, $num_words + 1 );
if ( count( $words_array ) > $num_words ) {
array_pop( $words_array );
$text = implode( $sep, $words_array );
$text = $text . $more;
$rest = implode( $sep, array_slice( $split_array, $num_words ) );
} else {
$text = implode( $sep, $words_array );
$rest = '';
}
/**
* 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 array( apply_filters( 'wp_trim_words_2', $text, $num_words, $more, $original_text ), $rest );
}
这样称呼:
list($trimmed, $rest) = wp_trim_words_2($text);
但我不确定apply_filters()
。是否应该通过$rest
?对我来说,研究这个对我来说太费时了,抱歉。
另一方面,如果您希望删除标记之前的$rest
(即在调用wp_strip_all_tags()
之前),那么事情要复杂得多,因为原始函数将输入分成之后的单词,我只能想到保持wp_strip_all_tags()
的输入和输出之间的对应关系的低效方法。
'/\X/u'
代替'/./u'
(阅读here以获得解释)。
答案 1 :(得分:0)
完全不确定是否需要调整该功能,此代码可以满足您的要求:
$trimmed_text = wp_trim_words($text, $wordlength, '');
// Measure full and trimmed widths for comparison
$fw = mb_strwidth($text);
$tw = mb_strwidth($trimmed_text);
if( $fw != $tw ){
$clipped_text = mb_strimwidth($text, $tw, $fw - $tw, '');
// Text has been cut
} else {
// Text has NOT been cut
}
或将其包装在您最初想要的功能中
function wp_trim_words_new($text, $length, $delimiter=''){
$trimmed_text = wp_trim_words($text, $length, $delimiter);
$fw = mb_strwidth($text);
$tw = mb_strwidth($trimmed_text);
if( $fw != $tw ){
return [$trimmed_text, mb_strimwidth($text, $tw, $fw - $tw, $delimiter)];
} else {
return [$text, null];
}
}