Skip to content

Commit

Permalink
Fix mb_ fallbacks and PCRE/u compat check
Browse files Browse the repository at this point in the history
Fixes 1915
  • Loading branch information
ozh committed Jun 5, 2015
1 parent c25f136 commit bd9ee4c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
8 changes: 4 additions & 4 deletions includes/functions-compat.php
Expand Up @@ -136,14 +136,14 @@ function mb_substr( $str, $start, $length = null, $encoding = null ) {
endif;
function yourls_mb_substr( $str, $start, $length = null, $encoding = null ) {
if ( null === $encoding ) {
$encoding = get_option( 'blog_charset' );
$encoding = 'UTF-8';
}
// The solution below works only for UTF-8,
// so in case of a different charset just use built-in substr()
if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
}
if ( _wp_can_use_pcre_u() ) {
if ( yourls_supports_pcre_u() ) {
// Use the regex unicode support to separate the UTF-8 characters into an array
preg_match_all( '/./us', $str, $match );
$chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
Expand Down Expand Up @@ -187,14 +187,14 @@ function mb_strlen( $str, $encoding = null ) {
endif;
function yourls_mb_strlen( $str, $encoding = null ) {
if ( null === $encoding ) {
$encoding = get_option( 'blog_charset' );
$encoding = 'UTF-8';
}
// The solution below works only for UTF-8,
// so in case of a different charset just use built-in strlen()
if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
return strlen( $str );
}
if ( _wp_can_use_pcre_u() ) {
if ( yourls_supports_pcre_u() ) {
// Use the regex unicode support to separate the UTF-8 characters into an array
preg_match_all( '/./us', $str, $match );
return count( $match[0] );
Expand Down
26 changes: 20 additions & 6 deletions includes/functions-formatting.php
Expand Up @@ -261,6 +261,25 @@ function yourls_seems_utf8( $str ) {
return true;
}


/**
* Check for PCRE /u modifier support. Stolen from WP.
*
* Just in case "PCRE is not compiled with PCRE_UTF8" which seems to happen
* on some distros even for PHP 5.3
*
* @since 1.7.1
*
* @return bool whether there's /u support or not
*/
function yourls_supports_pcre_u() {
static $utf8_pcre;
if( !isset( $utf8_pcre ) ) {
$utf8_pcre = (bool) @preg_match( '/^./u', 'a' );
}
return $utf8_pcre;
}

/**
* Checks for invalid UTF8 in a string. Stolen from WP
*
Expand All @@ -277,13 +296,8 @@ function yourls_check_invalid_utf8( $string, $strip = false ) {
return '';
}

// Check for support for utf8 in the installed PCRE library once and store the result in a static
static $utf8_pcre;
if ( !isset( $utf8_pcre ) ) {
$utf8_pcre = @preg_match( '/^./u', 'a' );
}
// We can't demand utf8 in the PCRE installation, so just return the string in those cases
if ( !$utf8_pcre ) {
if ( !$yourls_supports_pcre_u() ) {
return $string;
}

Expand Down

0 comments on commit bd9ee4c

Please sign in to comment.