php 修改wp_trim_words函数以返回内容拆分为2

izkcnapc  于 2023-02-03  发布在  PHP
关注(0)|答案(3)|浏览(105)

我试图修改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 );

}

hs1ihplo

hs1ihplo1#

在我看来,你只是简单地复制了WordPress的trim_words()函数,你本可以做得更好,你想要的输出不清楚。
假设您对与原始函数的输出一起返回的简单的无标记的剩余单词字符串感到满意,您可以使用如下代码(未经测试):

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也要过吗?我研究这个太费时间了,抱歉。
另一方面,如果您希望$restbefore 标记被删除(即在调用wp_strip_all_tags()之前),那么事情就复杂得多,因为原始函数将输入拆分为单词 after,我只能想到一些低效的方法来保持wp_strip_all_tags()的输入和输出之间的对应关系。
顺便说一句,WordPress的原始代码包含一个bug:东亚语言的单词被错误地计数(并可能被拆分!)为单个Unicode字符而不是Unicode字形。修复方法是使用'/\X/u'而不是'/./u'(阅读此处以获取解释)。

slwdgvem

slwdgvem2#

不确定是否需要调整该函数,下面的代码将执行您所要求的操作:

$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
}

或者将其 Package 在一个函数中,该函数的行为与您最初希望的一样

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];
    }
}
mpgws1up

mpgws1up3#

您可以在没有wp_trim_words函数的情况下完成此操作

$sentence = "This is a sample sentence";
$words = explode(" ", $sentence);
$half = count($words) / 2;
$first_half = implode(" ", array_slice($words, 0, $half));
$second_half = implode(" ", array_slice($words, $half));

echo $first_half . "\n";
echo $second_half;

相关问题