From bd9ee4c9f9ad197f049ee1b0a9863a7a3ef86bcf Mon Sep 17 00:00:00 2001 From: ozh Date: Fri, 5 Jun 2015 16:01:51 +0200 Subject: [PATCH] Fix mb_ fallbacks and PCRE/u compat check Fixes 1915 --- includes/functions-compat.php | 8 ++++---- includes/functions-formatting.php | 26 ++++++++++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/includes/functions-compat.php b/includes/functions-compat.php index 7829eb8..6db1b6b 100644 --- a/includes/functions-compat.php +++ b/includes/functions-compat.php @@ -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 ); @@ -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] ); diff --git a/includes/functions-formatting.php b/includes/functions-formatting.php index 2407d91..b288e8f 100644 --- a/includes/functions-formatting.php +++ b/includes/functions-formatting.php @@ -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 * @@ -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; } -- 2.45.0