WordPress.org

Make WordPress Core

Changeset 7140


Ignore:
Timestamp:
03/03/08 21:05:23 (7 years ago)
Author:
ryan
Message:

Multi-byte character safe excerpting from nbachiyski. fixes #6077

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/dashboard.php

    r7131 r7140  
    336336            else 
    337337                $content = __( 'something' ); 
    338             $content = strip_tags( $content ); 
    339             if ( 50 < strlen($content) ) 
    340                 $content = substr($content, 0, 50) . ' ...'; 
    341             $content = wp_specialchars( $content ); 
     338            $content = wp_html_excerpt($content, 50) . ' ...'; 
    342339            if ( $link ) 
    343340                $text = _c( '%1$s linked here <a href="%2$s">saying</a>, "%3$s"|feed_display' ); 
  • trunk/wp-includes/comment.php

    r6994 r7140  
    10471047        $excerpt = apply_filters('the_excerpt', $post->post_excerpt); 
    10481048    $excerpt = str_replace(']]>', ']]&gt;', $excerpt); 
    1049     $excerpt = strip_tags($excerpt); 
    1050     if ( function_exists('mb_strcut') ) // For international trackbacks 
    1051         $excerpt = mb_strcut($excerpt, 0, 252, get_option('blog_charset')) . '...'; 
    1052     else 
    1053         $excerpt = substr($excerpt, 0, 252) . '...'; 
     1049    $excerpt = wp_html_excerpt($excerpt, 252) . '...'; 
    10541050 
    10551051    $post_title = apply_filters('the_title', $post->post_title); 
  • trunk/wp-includes/compat.php

    r6726 r7140  
    7878endif; 
    7979 
     80if ( ! function_exists('mb_strcut') ): 
     81    function mb_strcut( $str, $start, $length=null, $encoding=null ) { 
     82        return _mb_strcut($str, $start, $length, $encoding); 
     83    } 
     84endif; 
     85 
     86function _mb_strcut( $str, $start, $length=null, $encoding=null ) { 
     87    // the solution below, works only for utf-8, so in case of a different 
     88    // charset, just use built-in substr 
     89    $charset = get_option( 'blog_charset' ); 
     90    if ( !in_array( $charset, array('utf8', 'utf-8', 'UTF8', 'UTF-8') ) ) { 
     91        return is_null( $length )? substr( $str, $start ) : substr( $str, $start, $length); 
     92    } 
     93    // use the regex unicode support to separate the UTF-8 characters into an array 
     94    preg_match_all( '/./us', $str, $match ); 
     95    $chars = is_null( $length )? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length ); 
     96    return implode( '', $chars ); 
     97} 
     98 
    8099?> 
  • trunk/wp-includes/formatting.php

    r7056 r7140  
    13711371} 
    13721372 
     1373/** 
     1374 * Safely extracts not more than the first $count characters from html string 
     1375 * 
     1376 * UTF-8, tags and entities safe prefix extraction. Entities inside will be 
     1377 * counted as one character. As a side effect, all entities will be converted to 
     1378 * their decimal form. 
     1379 * 
     1380 * @param integer $str String to get the excerpt from 
     1381 * @param integer $count Maximum number of visible characters to take 
     1382 * @eaturn string the excerpt 
     1383 */ 
     1384function wp_html_excerpt( $str, $count ) { 
     1385    $str = strip_tags( $str ); 
     1386    $str = html_entity_decode( $str, ENT_QUOTES); 
     1387    $str = mb_strcut( $str, 0, $count ); 
     1388    // remove part of an entity at the end 
     1389    $str = preg_replace( '/&[^;\s]{0,6}$/', '', $str ); 
     1390    // we decoded some entities we should put back 
     1391    $str = wp_specialchars( $str ); 
     1392    return $str; 
     1393} 
     1394 
    13731395?> 
  • trunk/wp-settings.php

    r6982 r7140  
    1515if ( function_exists('memory_get_usage') && ( (int) @ini_get('memory_limit') < abs(intval(WP_MEMORY_LIMIT)) ) ) 
    1616    @ini_set('memory_limit', WP_MEMORY_LIMIT); 
     17 
    1718 
    1819/** 
     
    351352require (ABSPATH . WPINC . '/pluggable.php'); 
    352353 
     354/* 
     355 * In most cases the default internal encoding is latin1, which is of no use, 
     356 * since we want to use the mb_ functions for utf-8 strings 
     357 */ 
     358if ( function_exists('mb_internal_encoding') ) 
     359    mb_internal_encoding( get_option( 'blog_charset' ) ); 
     360 
     361 
     362 
    353363if ( defined('WP_CACHE') && function_exists('wp_cache_postload') ) 
    354364    wp_cache_postload(); 
  • trunk/wp-trackback.php

    r6716 r7140  
    7373        trackback_response(1, 'Sorry, trackbacks are closed for this item.'); 
    7474 
    75     $title =  wp_specialchars( strip_tags( $title ) ); 
    76     $excerpt = strip_tags($excerpt); 
    77     if ( function_exists('mb_strcut') ) { // For international trackbacks 
    78         $excerpt = mb_strcut($excerpt, 0, 252, get_option('blog_charset')) . '...'; 
    79         $title = mb_strcut($title, 0, 250, get_option('blog_charset')) . '...'; 
    80     } else { 
    81         $excerpt = (strlen($excerpt) > 255) ? substr($excerpt, 0, 252) . '...' : $excerpt; 
    82         $title = (strlen($title) > 250) ? substr($title, 0, 250) . '...' : $title; 
    83     } 
     75    $title =  wp_html_excerpt( $title, 250 ).'...'; 
     76    $excerpt = wp_html_excerpt( $excerpt, 252 ).'...'; 
    8477 
    8578    $comment_post_ID = (int) $tb_id; 
Note: See TracChangeset for help on using the changeset viewer.