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

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 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?>